forked from changqing16/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path202.cpp
More file actions
42 lines (39 loc) · 753 Bytes
/
202.cpp
File metadata and controls
42 lines (39 loc) · 753 Bytes
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
#include <stdio.h>
int digitSquareSum(int n)
{
int sum = 0, tmp;
while (n)
{
tmp = n % 10;
sum += tmp * tmp;
n /= 10;
}
return sum;
}
bool isHappy(int n)
{
int slow, fast;
slow = fast = n;
do
{
slow = digitSquareSum(slow); //根据题意,如果不是的话,将是无限循环,可以用判断链表的方法来判断是否是无限循环
fast = digitSquareSum(fast); //厉害厉害!!!
fast = digitSquareSum(fast);
if (fast == 1)
return 1;
} while (slow != fast);
return 0;
}
int main()
{
bool temp = isHappy(2);
if (temp)
{
printf("true");
}
else
{
printf("false");
}
return 0;
}