각 동전간 배수 관계 => 그리디 형식으로, 가장 큰 동전부터 빼는 작업이 가능함
정답 코드 : for문 사용
arr = [50000, 10000, 5000, 1000, 500, 100, 50, 10]
t= int(input())
for tc in range(1,t+1):
n = int(input())
cnt = [0] * 8
for i in range(8):
if n // arr[i] >0:
cnt[i] += n // arr[i]
n = n % arr[i]
print(f'#{tc}')
print(*cnt)
틀린 코드 while 사용 으로 런타임 오류로 2/10 개 맞춤
arr = [50000, 10000, 5000, 1000, 500, 100, 50, 10]
t= int(input())
for tc in range(1,t+1):
n = int(input())
cnt = [0] * 8
i = 0
while n >0 or i <= 7:
if n >= arr[i]:
n -= arr[i]
cnt[i] += 1
else:
i += 1
print(f'#{tc}')
print(*cnt)
'알고리즘 문제풀이' 카테고리의 다른 글
백준 5622. 다이얼 Python 구현 브론즈2 (0) | 2024.03.04 |
---|---|
백준 11650.좌표 정렬하기 Python 정렬 실버5 (0) | 2024.03.04 |
Swea D3_11718. 사냥꾼 Python (0) | 2024.03.02 |
Swea D3_11671. 기지국 Python (0) | 2024.03.02 |
Swea D4. 반사경 Python (1) | 2024.02.29 |