Skip to content

Codeforces 暑期特訓:我想成為演算法大師 - 2017-2018 ACM-ICPC, Asia Daejeon Regional Contest - Week 10

2017-2018 ACM-ICPC, Asia Daejeon Regional Contest

Happy Number

Problem: Happy Number

Solution: GitHub Code

python
n = int(input())
nn = n

visit = []
ans = False
while True:
    x = 0
    for i in str(nn):
        j = int(i)
        x += j ** 2
    nn = x

    if x in visit:
        break
    else:
        visit.append(x)

    if x == 1:
        ans = True
        break
    elif x == n:
        break

if ans:
    print('HAPPY')
else:
    print('UNHAPPY')