🙌 출처 - 프로그래머스 🙌
문제 설명
자연수 n이 주어졌을 때, n의 다음 큰 숫자는 다음과 같이 정의 합니다.
예를 들어서 78(1001110)의 다음 큰 숫자는 83(1010011)입니다.
자연수 n이 매개변수로 주어질 때, n의 다음 큰 숫자를 return 하는 solution 함수를 완성해주세요.
제한 조건
입출력 예
n | result |
---|---|
78 | 83 |
15 | 23 |
입출력 예 설명
입출력 예#1
입출력 예#2
- format 함수를 이용해 2진수로 변환하고, 그 값안에 1의 개수를 count 함수를 이용해 check 해주었습니다.
- 만약 1의 숫자가 다르다면 숫자를 1씩 증가해주면서 계속 비교해 주었습니다.
def solution(n):
num = n + 1
while format(n, 'b').count('1') != format(num, 'b').count('1'):
num += 1
return num
def nextBigNumber(n):
c = bin(n).count('1')
for m in range(n+1,1000001):
if bin(m).count('1') == c:
return m
1. 2,8,16진법을 10진법으로 변환하기
a1=int('aa',16)
a2=int('56',8)
a3=int('10',2)
print(a1)
print(a2)
print(a3)
< 결과 >
170
46
2
2. 10진법을 2진법으로 변환하기
a1=bin(30)
a2=bin(15)
a3=bin(10)
print(a1)
print(a2)
print(a3)
< 결과 >
0b11110
0b1111
0b1010
3. 10진법을 2진법으로 변환하는데 앞에 0b 제거하기
myNumber=[10,20,30,40,50]
for n in myNumber:
print(bin(n)[2:])
< 결과 >
1010
10100
11110
101000
110010
4. 10진법을 8진법으로 변환하기
oct(n)
5. 10진법을 16진법으로 변환하기
hex(n)
10진수, 2진수, 8진수, 16진수 출력
# 방법 1
print("### format ###")
print('{:#o}'.format(10))
print('{:#x}'.format(10))
print('{:#b}'.format(10))
# 방법 2
print("### format, notation ###")
print(format(10, 'b')) #2진수 변환
print(format(10, 'o')) #8진수 변환
print(format(10, 'x')) #16진수 변환
### format ###
0o12
0xa
0b1010
### format, notation ###
1010
12
a