Write an algorithm to determine if a number is "happy".
A happy number is a number defined by the following process:starting with any positive integer and replace the number by the Sum of the squares of its digits, and repeat the process until the number equals 1 (where it would stay), or it loops Endl essly in a cycle which does not include 1. Those numbers for which this process ends in 1 is happy numbers.
Example:19 is a happy number
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
This question asks whether a number is a "happiness number".
The so-called happiness number, is according to the above rules, in order to find the number of n each bit of the sum of squares, if the sum is equal to 1, then n is the number of happiness, if not equal to 1, then the sum is assigned to N, continue to seek the square sum. Until the result is judged.
It is important to note that if a number is the number of happiness, then it is bound to appear and the result of 1 can jump out of the loop. And if a number is not a number of happiness, it may have been recycled. So to find out the conditions of the loop: You can find that when the sum of squares is equal to not 1 of a number (0,2--9), you can jump out of the loop, make not the number of happiness judgment.
Class Solution { Public:BOOL Ishappy(intN) {intAns =0; while(true){ while(n) {ans + = POW (n%Ten,2); N/=Ten; }if(ans = =1)return true;Else if(ANS/Ten==0)return false;Else{n = ans; Ans =0; } } }};
[Leetcode] Happy number