๋ค์ ํฐ ์ซ์
https://programmers.co.kr/learn/courses/30/lessons/12911
์ฝ๋ฉํ ์คํธ ์ฐ์ต - ๋ค์ ํฐ ์ซ์
์์ฐ์ n์ด ์ฃผ์ด์ก์ ๋, n์ ๋ค์ ํฐ ์ซ์๋ ๋ค์๊ณผ ๊ฐ์ด ์ ์ ํฉ๋๋ค. ์กฐ๊ฑด 1. n์ ๋ค์ ํฐ ์ซ์๋ n๋ณด๋ค ํฐ ์์ฐ์ ์ ๋๋ค. ์กฐ๊ฑด 2. n์ ๋ค์ ํฐ ์ซ์์ n์ 2์ง์๋ก ๋ณํํ์ ๋ 1์ ๊ฐฏ์๊ฐ ๊ฐ์ต๋
programmers.co.kr
๋ฌธ์ ์ค๋ช
์์ฐ์ n์ด ์ฃผ์ด์ก์ ๋, n์ ๋ค์ ํฐ ์ซ์๋ ๋ค์๊ณผ ๊ฐ์ด ์ ์ ํฉ๋๋ค.
- ์กฐ๊ฑด 1. n์ ๋ค์ ํฐ ์ซ์๋ n๋ณด๋ค ํฐ ์์ฐ์ ์ ๋๋ค.
- ์กฐ๊ฑด 2. n์ ๋ค์ ํฐ ์ซ์์ n์ 2์ง์๋ก ๋ณํํ์ ๋ 1์ ๊ฐฏ์๊ฐ ๊ฐ์ต๋๋ค.
- ์กฐ๊ฑด 3. n์ ๋ค์ ํฐ ์ซ์๋ ์กฐ๊ฑด 1, 2๋ฅผ ๋ง์กฑํ๋ ์ ์ค ๊ฐ์ฅ ์์ ์ ์ ๋๋ค.
์๋ฅผ ๋ค์ด์ 78(1001110)์ ๋ค์ ํฐ ์ซ์๋ 83(1010011)์ ๋๋ค.
์์ฐ์ n์ด ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, n์ ๋ค์ ํฐ ์ซ์๋ฅผ return ํ๋ solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
์ ํ ์ฌํญ
- n์ 1,000,000 ์ดํ์ ์์ฐ์ ์ ๋๋ค.
ํ์ด
def solution(n):
q = bin(n)[2:].count('1')
while 1:
n += 1
if bin(n)[2:].count('1') == q :
return n
์ฌ์ฉ๋ ๊ฐ๋
* 10์ง์ -> 2์ง์, 8์ง์, 16์ง์๋ก ๋ณํ
'Developer > ๐ ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค/python] - ํํ (0) | 2020.06.21 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค/python] - JadenCase ๋ฌธ์์ด ๋ง๋ค๊ธฐ (0) | 2020.06.21 |
[ํ๋ก๊ทธ๋๋จธ์ค/python] ์์ฅ (0) | 2020.06.18 |
[ํ๋ก๊ทธ๋๋จธ์ค/python] ๊ธฐ๋ฅ๊ฐ๋ฐ (0) | 2020.06.18 |
[ํ๋ก๊ทธ๋๋จธ์ค/python] ์ ํ๋ฒํธ ๋ชฉ๋ก (0) | 2020.06.17 |