programmers.co.kr/learn/courses/30/lessons/12911
λμ λ¬Έμ μ ! bin() μ μμΌλ€!!!!!!!!!!!!!!!!!!!!!!!!!!!!! κ·Έλμ!!!!!!!!!!!!! μ΄μ§μλ₯Ό νλμ© λ€ λ§λ€μ΄μ€¬λ€ λ©μ²νλ©΄ λͺΈμ΄ κ³ μνλ€λλ..
π μ€μν μ¬μ€
- bin(n) : 2μ§μλ‘ λ§λ€μ΄μ€. return type = str
- hex(n) : 16μ§μλ‘ λ§λ€μ΄μ€. return type = str
- oct(n) : 8μ§μλ‘ λ§λ€μ΄μ€. return type = str
- int(x, y) : x = λ³κ²½νλ €λ μ«μ, y = λ³κ²½νλ €λ μ«μμ μ§μ
- int("0b1011", 2) = 2μ§μ 1011μ 10μ§μλ‘ λ°κΎΈκ² λ€ = κ²°κ³Όκ° : 11
πβοΈ μμ±ν μ½λ (bin μ°μ§μκ³ 2μ§μλ‘ λ°κΏ)
λ보기
def solution(n):
answer = 0
def convert_bin(n):
n_binary = ''
while n > 0:
n_binary += str(n%2)
n = n // 2
n_binary = n_binary[::-1] #μμ λ ¬
return n_binary
bin_n = convert_bin(n)
cnt = bin_n.count('1') #nμ 2μ§μλ‘ λ³νν κ²μμ 1μ κ°―μ
while 1:
n += 1 #μ«μλ₯Ό 1μ© μ¬λ¦Ό
tmp_cnt = convert_bin(n).count('1')
if cnt == tmp_cnt : #1μ κ°―μκ° κ°μΌλ©΄
answer = n
break
return answer
πβοΈ λ λ©μ§ μ½λ (bin(n) μ¬μ©)
λ보기
def solution(n):
answer = 0
n_cnt = bin(n).count('1')
while 1:
n += 1
tmp_cnt = bin(n).count('1')
if tmp_cnt == n_cnt:
answer = n
break
return answer
'πκ³΅λΆ > μκ³ λ¦¬μ¦' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λ°±μ€] 2447λ² λ³μ°κΈ°-10 (0) | 2020.12.19 |
---|---|
[λ°±μ€] 2869λ² λ¬ν½μ΄λ μ¬λΌκ°κ³ μΆλ€ (0) | 2020.12.06 |
[νλ‘κ·Έλλ¨Έμ€ Lv.2] μ¬λ°λ₯Έ κ΄νΈ (0) | 2020.11.30 |
[λ°±μ€] 2110λ² κ³΅μ κΈ° μ€μΉ (0) | 2020.11.24 |
[λ°±μ€] 2805λ² λ무 μλ₯΄κΈ° (0) | 2020.11.24 |
λκΈ