算法珠玑——双百故事

https://leetcode-cn.com/problems/happy-number/submissions/

class Solution {
public:
    bool isHappy(int n) {
        short times = 7;
        int num;
        while(n != 1 && --times)
        {
            num = 0;
            while(n) {
                num+=(n%10)*(n%10);
                n/=10;
            }
            n = num;
        }
        if (n == 1) return true;
        return false;
    }
};
posted @ 2022-04-11 16:22  千心  阅读(30)  评论(0编辑  收藏  举报