1212번 : 8진수와 2진수 - Python

Pobi·2023년 2월 12일
0

PS

목록 보기
43/107

문제

https://www.acmicpc.net/problem/1212

풀이

python의 int함수를 이용해서 8진수의 수를 10진수로 바꾼다음에 bin함수를 이용해서 2진수로 바꾼다.

코드

from sys import stdin

input = stdin.readline

s = input().strip()

print(bin(int(s,8))[2:])

다른 풀이

8진수에서 1자리로 나타낼수 있는 0~7을 2진수로 저장한다음 8진수 한자리씩 2진수로 바꾼다.

다른 코드

from sys import stdin

input = stdin.readline

eight=input()

two = ["000", "001", "010", "011", "100", "101", "110", "111" ] # 8진수들을 2진수로 표현하는 경우의 수

temp=0

for i in range(len(eight)):
    temp = int(eight[i])

    if i==0:
        print(int(two[temp]),end='') #0이 아니면 0으로 시작하면 안되기때문에 int로 001 같은 것들을 1로 출력해준다.
    else:
        print(two[temp],end='')

다른 풀이

https://blockdmask.tistory.com/243 이곳에서의 1번째 방법처럼 미리 저장하는 방식이 아닌 하나씩 계산하는 방식이 있다.

profile
꿈 많은 개발자

0개의 댓글