2331번 : 반복수열 - Python

Pobi·2023년 3월 31일
0

PS

목록 보기
53/107

문제

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

풀이

다음 항(n)을 계산한 다음 n이 순열에 있는지 확인하고, 없다면 순열에 넣고, 있다면 n의 index를 출력한다.(n이 있다는 것은 n부터 반복된다는걸 의미한다.)

코드

from sys import stdin

input = stdin.readline

#수열 계산하는 함수
def cal(n,p):
    c = 0
    while(n!=0):
        c += (n%10)**p
        n//=10

    return c

a, p = map(int,input().split())

d = list()
d.append(a)

while(1):
    n = cal(d[-1],p)

    if n in d: #다음 항(n)이 수열에 있다면 n의 index를 출력한다.
        i = d.index(n)
        print(i)
        break
    else: #n이 수열에 없다면 수열에 추가한다.
        d.append(n)
profile
꿈 많은 개발자

0개의 댓글