class Solution { public: int nextStep(int cur) { int res = 0; while(cur) { res += (cur % 10) * (cur % 10); cur /= 10; } return res; } //根据鸽巢原理,变换过程一定成环 //若为快乐数,环中数据全部为1 //否则不为快乐数 bool happynum(int n) { int fast = n, slow = n; do { fast = nextStep(nextStep(fast)); slow = nextStep(slow); } while(fast != slow); return fast == 1; } };