본문 바로가기
python-algorithm

백준 4388 받아올림

by 무적김두칠 2023. 12. 27.

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

 

4388번: 받아올림

어린이에게 여러자리 숫자의 덧셈을 가르칠 때는 오른쪽 자리부터 왼쪽으로 하나씩 계산하는 방법을 가르쳐준다. 이때, 받아올림이 발생하게 되며 아이들은 여기서 혼란에 빠진다. 받아올림이

www.acmicpc.net

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def sol(n1, n2):
    n1 = n1.zfill(max(len(n1), len(n2)))[::-1]
    n2 = n2.zfill(max(len(n1), len(n2)))[::-1]
    answer = 0
    carry = 0
    for i in range(len(n1)):
        if int(n1[i]) + int(n2[i]) + carry >= 10:
            answer += 1
            carry = 1
        else:
            carry = 0
    return answer
 
 
if __name__ == '__main__':
 
    while True:
        n1, n2 = input().split()
        if n1 == '0' and n2 == '0':
            break
        print(sol(n1, n2))
 
cs
반응형

'python-algorithm' 카테고리의 다른 글

leetcode 2956. Find Common Elements Between Two Arrays  (0) 2023.12.27
leetcode 2965. Find Missing and Repeated Values  (0) 2023.12.27
백준 6550 부분 문자열  (2) 2023.12.23
백준 11723 집합  (1) 2023.12.23
백준 30045 ZOAC 6  (0) 2023.12.20

댓글