입력
정수 N (0 ≤ N ≤ 100000000)이 입력된다.
0 이상의 정수가 주어졌을 때, 정수를 앞자리부터 차례대로 끊어서 출력하고, 마지막 줄에는 각 자릿수의 합을 출력하라. 예를 들어 1023이 입력된다면, 1, 0, 2, 3, 6을 출력하면 된다.
입력은 항상 정확하다.
N
출력
N1
N2
N3
...
Nl
S
예제 입출력
기본 1 | 1023 | 1 0 2 3 6 |
6 = 1 + 0 + 2 + 3 |
기본 2 | 12 | 1 2 3 |
3 = 1 + 2 |
기본 3 | 32524300 | 3 2 5 2 4 3 0 0 19 |
19 = 3 + 2 + 5 + 2 + 4 + 3 + 0 + 0 |
경계값 1 | 0 | 0 0 |
|
경계값 2 | 100000000 | 1 0 0 0 0 0 0 0 0 1 |
풀이
N = int(input())
numbers = str(N) # int형은 자릿수 직접 순회못해서 str로
total = 0
for number in numbers:
print(int(number))
total += int(number)
print(total)
N = 12345
total = 0
current = N
digits = []
while True:
digit = current % 10
digits.append(digit) # digits.insert(0, digit)
current = current // 10
if current == 0:
break
digits.reverse()
digits_sum = sum(digits)
for digit in digits:
print(digit)
print(digits_sum)
'Data Structure > 백준 문제' 카테고리의 다른 글
(Python) 달팽이 배열 (0) | 2025.04.22 |
---|---|
(Python) 더하기 사이클 (0) | 2025.04.10 |
(Python) 콜라츠의 추측 (0) | 2025.04.04 |
(Python) 별 찍기 (0) | 2025.03.30 |
행렬 (0) | 2025.01.14 |