[백준 1110 파이썬] - 더하기 사이클

zsunny·2022년 6월 16일
0

📌 문제

💯 정답

방법 1. int형으로 푸는 방법

import sys
input = sys.stdin.readline

n = int(input())
origin = n
cnt = 0

while True:
    n = (n%10)*10 + ((n//10)+(n%10))%10
    cnt += 1
    if n == origin:
        break

print(cnt)

방법 2. 문자열로 푸는 방법 -> 런타임에러

import sys
input = sys.stdin.readline

n = input().rstrip()
origin = n
cnt = 0

while True:
    if len(origin) == 1:
        n = "0" + n
    calc = str(int(n[0]) + int(n[1]))		//두자리 덧셈
    n = n[-1] + calc[-1]					//이어붙인 수
    cnt += 1
    if n == origin:
        break;

print(cnt)

📝 설명

• 방법 1. int형으로 푸는 방법
  각 자릿수를 // 와 % 를 이용해 나타낸다.
    
• 방법 2. 문자열로 푸는 방법
  []를 사용해 문자열의 자릿수에 접근한다.
  이 경우 런타임에러가 뜨는데 예제 입력3의 "1"의 경우 사이클이 도는데 시간이 오래 걸리는 것 같다.
profile
매일 성장하는 예비 웹 개발자 🌱

0개의 댓글