【題目敘述】https://zerojudge.tw/ShowProblem?problemid=d442
#include <iostream>
#include <set>
using namespace std;
int main() {
int T, Case=1;
long long N, tmp;
cin >> T;
while (T--){
cin >> N;
cout << "Case #" << Case++ << ": " << N;
set<long long> st;
while (N != 1){
if (st.count(N)){
break;
}
st.insert(N);
tmp = 0;
while (N >= 10){
tmp += (N % 10) * (N % 10);
N /= 10;
}
tmp += N*N;
N = tmp;
}
if (N == 1) cout << " is a Happy number.\n";
else cout << " is an Unhappy number.\n";
}
return 0;
}
Python code (credit: Amy Chou)
T = int(input())
for TC in range(1, T+1):
N = int(input())
print(f"Case #{TC}: {N} ", end="")
st = set()
while N != 1:
if N in st:
break
st.add(N)
tmp = 0
while N >= 10:
tmp += (N % 10) ** 2
N //= 10
tmp += N * N
N = tmp
if N == 1:
print("is a Happy number.")
else:
print("is an Unhappy number.")