LintCode: Happy Number

C++

 1 class Solution {
 2 public:
 3     /**
 4      * @param n an integer
 5      * @return true if this is a happy number or false
 6      */
 7     bool isHappy(int n) {
 8         // Write your code here
 9         int a, b;
10         if (n <= 0) return false;
11         if (n < 10) n = n*n;
12         while (n > 9) {
13             a = 0;
14             while (n) {
15                 b = n%10;
16                 a += b*b;
17                 n /= 10;
18             }
19             n = a;
20         }
21         return n == 1;
22     }
23 };
原文地址:https://www.cnblogs.com/CheeseZH/p/5105353.html