문제 : https://www.acmicpc.net/problem/1373
- 주어진 2진수를 3자리씩 끊은다음에
- 각 자리수에 4, 2, 1을 곱해줘서 자릿수마다 더해주면 8진수이다.
N = input()[::-1]
ans = ''
tmp = 0 # 각 자릿수의 계산 결과를 담을 변수
cnt = 1 # 곱해줄 배수 : 1,2,4
for i in N:
if(cnt < 8):
tmp += cnt * int(i)
cnt *= 2
else:
ans += str(tmp)
tmp = 1 * int(i)
cnt = 2
ans += str(tmp)
print(int(ans[::-1]))
input()[::-1]
로 값을 거꾸로 받을 수도 있다는 것을 알게되었다.print(oct(int(input(),2))[2:])
int(input(),2)
: 2진수를 10진수로 변환 (입력은 2진수로 할거다 이런 뜻)oct()
: 10진수를 8진수로 변환hex()
, bin()
[2:]
로 슬라이싱