https://www.acmicpc.net/problem/1110
처음에는 단순하게 문자열로 처리해서 숫자끼리 더하고 원래 숫자와 비교하려고 했는데, 시간초과가 발생했다.
- 시간초과 발생한 풀이
n = input()
num = n
count = 0
while True:
if len(num) == 1:
num = '0' + num
count += 1
new_n = str(int(num[0]) + int(num[1]))
num = num[-1] + new_n[-1]
if num == n:
print(count)
break
처음부터 int형으로 받고 10으로 나눠서 나머지끼리 더하면 괜찮을 것 같아서 시도해보았다.
n = int(input())
num = n
count = 0
while True:
count += 1
plus = num // 10 + num % 10
num = num % 10 * 10 + plus % 10
if num == n:
break
print(count)
다행히 시간초과가 발생하지 않고 맞췄다!
Uploaded by Notion2Tistory v1.1.0