• Home
  • About
    • JOOS photo

      JOOS

      Joos's blog

    • Learn More
    • Email
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

[백준] 1373번 : 2진수 8진수 with python3

30 Sep 2018

Reading time ~1 minute

문제: https://www.acmicpc.net/problem/1373

import sys
read = lambda : sys.stdin.readline().strip()

b = read()

if len(b)%3:
    b = '0'*(3-len(b)%3) + b
# b의 길이가 3의 배수가 아니면 그 때 0이 붙게 하는 것이다.
ans = ''
for i in range(0,len(b),3):
    tmp = b[i:i+3]
    ans += str(int(tmp[0])*4+int(tmp[1])*2+int(tmp[2])*1)

print(ans)

8진법은 2진법의 3자리 수 인 것끼리 더한 값을 한 자리수로 만든 것이다. 그래서 3자리수씩 뽑아서 10진법으로 만들어서 8진법의 자리수를 채우면된다.



algorithm백준 Share Tweet +1