알고리즘

개미수열 알고리즘 파이썬

moonsun623 2021. 10. 15. 17:58
반응형

개미수열이란?

앞의 수를 연속된 같은 수의 개수로 묶어서 읽는 방식으로 만들어지는 수열로

1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211 이런 식으로 진행된다.

 

개미수열 알고리즘(파이썬)

n=int(input())

s='1' #수열의 시작이 1이기 때문에
for i in range(n):
    x=s[0] 
    count=1
    s_dev=''
    for j in range(1,len(s)):
        if s[j]!=x:
            s_dev+=str(count)+str(x)
            x=s[j]
            count=1
        else:
            count+=1
    s=s_dev+str(count)+str(x)
print(s)