202. Happy Number

来源:互联网 发布:门捷列夫没得奖 知乎 编辑:程序博客网 时间:2024/05/29 16:10
class Solution {private:    int mysum(int n)    {        int res=0;        while(n)        {            int temp=n%10;            n/=10;            res+=temp*temp;        }        return res;    }public:    bool isHappy(int n) {        int slow=n;        int fast=n;        do{            slow=mysum(slow);            fast=mysum(fast);            fast=mysum(fast);            if(slow==1||fast==1)                break;        }while(slow!=fast);        if(slow==1||fast==1)            return true;        else             return false;    }};
1 0
原创粉丝点击