Happy Number

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main()
 5 {
 6     long long n, x, sum;
 7     scanf("%lld", &n);
 8     x = n;
 9 
10     sum = 0;
11     while(x>0)
12     {
13         sum = sum + (x%10) * (x%10);
14         x /= 10;
15     }
16     x = sum;
17 
18     while(x!=n&&x!=4&&x!=1)
19     {
20         sum = 0;
21         while(x>0)
22         {
23             sum = sum + (x%10) * (x%10);
24             x /= 10;
25         }
26         x = sum;
27     }
28     if(x==1) printf("HAPPY\n");
29     else printf("UNHAPPY\n");
30     return 0;
31 }

 

posted @ 2019-10-09 20:20  Xxiaoyu  阅读(133)  评论(0编辑  收藏  举报