ブリブリ備忘録 おっ、python

HackerRankの問題とコメント(python3) 拙いですが...

Print Function import math

・問題

Read an integer .

Without using any string methods, try to print the following:

 

Note that "" represents the values in between.

Input Format

The first line contains an integer .

Output Format

Output the answer as explained in the task.

Sample Input 0

3

Sample Output 0

123

ソースコード

import math
if __name__ == '__main__':
    n = int(input())
a=1
b=0
while n>=a:
    c=int(math.log10(a))+1
    b=b*(10**c)+a
    a=a+1
print (b)

・コメント

サンプルが1,2,3なので、12=1×10+2, 123=12×10+3というように前の数に10をかけて足せばよいと考えたが、1,2,3,・・・・,10などとなった時に、うまくいかなかったので、アルゴリズムを変更した。

c=int(math.log10(a))+1

 の部分aの桁数を求め、それを前の数にかけることによって問題を解決した。

さらに、logの関数を用いれるように最初に

import math

 を挿入する必要があることに注意。

・URL

https://www.hackerrank.com/challenges/python-print/problem