문제 주소 : https://www.acmicpc.net/problem/4571
정답 비율 : 85.714% (2021.12.19 15:20 기준)
4571번: Grade School Multiplication
An educational software company, All Computer Math (ACM), has a section on multiplication of integers. They want to display the calculations in the traditional grade school format, like the following computation of 432 × 5678: 432 5678 ---
www.acmicpc.net
<< 문제 요약 >>
두 수가 주어질 때, 두 수의 곱셈 과정을 나타내세요.
예로 두 수가 432, 5678일 때 출력을 다음 그림처럼 나타나야 합니다.
추가로 n번째 자릿수가 0인 경우에 대해서는 생략하여 나타냅니다.
예를 들어 두 수가 200001, 90040일 때 출력을 다음 그림처럼 나타나야 합니다.
<< 해결 과제 분석 >>
1. 두 수의 곱만큼 "-"를 출력해야 함.
2. A x B라고 했을 때 B를 0 기준으로 나눠야 함 ex) 201030 -> 20, 10, 30
3. B를 나눈 것들과 A를 곱셈한 결과를 앞에서 몇 칸의 공백을 출력해야 하는가?
4. 출력에 Problem #을 출력해야 함.
5. 1 ~ 4을 이용하여 출력
<< Python3 코드 >>
count = 1 # problem number
while True:
A, B = list(map(int, input().split()))
if A == B == 0:
break
# solve 1
totalLen = len(str(A * B))
# solve 2
splitB = [] # 2-set (number, idx)
tmpNum = ""
for idx, digit in enumerate(reversed(str(B))):
if digit == "0":
tmpNum = tmpNum + "0"
else:
splitB.append((int(digit + tmpNum), idx - len(tmpNum)))
tmpNum = ""
# solve 3 & 4
print("Problem", count)
print(" " * (totalLen - len(str(A))), A, sep='')
print(" " * (totalLen - len(str(B))), B, sep='')
print("-" * totalLen)
if len(splitB) == 1:
print(A * B)
else:
for num, i in splitB:
print(" " * (totalLen - len(str(A * num)) - i), A * num, sep='')
print("-" * totalLen)
print(A * B)
count += 1
'Algorithm Trainning > 백준 알고리즘(BEAKJOON)' 카테고리의 다른 글
백준 : 21147번, Triangular Collection (0) | 2022.01.06 |
---|---|
백준 : 6494번, Another lottery (0) | 2021.12.20 |
백준 : 10757번, 큰 수 A+B (0) | 2021.07.26 |
백준 : 7576번, 토마토 (0) | 2021.07.05 |
백준 : 2292번, 벌집 (0) | 2021.06.29 |