0
0

10進数から2進数に変換

Posted at

たとえば
1だったら 1
2だったら繰り上がるので 10
3だったら 11
4だったら繰り上がるので100
こんな感じ。

(pythonにpow()メソッドがあるのは知らないという設定で)

def make_pow(x,y):
    ret = 1
    for i in range(y):
        ret *= x
    return ret

N = int(input())

binary = 0
i = 0
while N > 0:
    digit = N % 2
    binary += digit * make_pow(10,i)
#    binary += digit * pow(10, i)
    N //= 2
    i += 1

print(binary)
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0