11576번 : Base Conversion - Python

Pobi·2023년 2월 21일
0

PS

목록 보기
44/107

문제

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

풀이

입력된 수를 10진법으로 바꾼뒤 B진법으로 바꾸면 된다. 여기서 입력된 수를 뒤에서부터 계산하여서 10진법으로 바꿔야 한다.

코드

from sys import stdin

input = stdin.readline

a, b = map(int,input().split())
m = int(input())
array = list(map(int,input().split()))

n = 0
#a진법의 수를 10진수로 변환한다.
array.reverse()#진법을 변환하려면 뒤에서 부터 읽어야 하기 때문에 reverse한다.
for i in range(m):
    n += array[i] * (a**i)

#10진법 n을 b진법으로 변환한다.
change = list()
while n!=0:
    change.append(n%b)
    n=n//b

print(*change)

profile
꿈 많은 개발자

0개의 댓글