-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva10591.cpp
More file actions
52 lines (42 loc) · 1 KB
/
uva10591.cpp
File metadata and controls
52 lines (42 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <cstdio>
#include <cmath>
#include <cstring>
bool visited[1000]; // The sum of digit of 10^9 must < 1000;
int sum_of_square_digit(int number) {
int sum = 0;
int tnum = number;
int tmp;
while(tnum > 0) {
tmp = tnum%10;
sum += tmp * tmp;
tnum /= 10;
}
sum += tnum * tnum;
return sum;
}
bool ishappy(int number) {
memset(visited, false, sizeof(visited));
int tnum = number;
tnum = sum_of_square_digit(tnum);
while(!visited[tnum]) {
visited[tnum] = true;
if(tnum == 1) return true;
tnum = sum_of_square_digit(tnum);
}
return false;
}
int main() {
// freopen("input.txt", "r", stdin);
int n, v;
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", &v);
bool result = ishappy(v);
if(result) {
printf("Case #%d: %d is a Happy number.\n", i, v);
} else {
printf("Case #%d: %d is an Unhappy number.\n", i, v);
}
}
return 0;
}