bool isHappy(int n)
{
if (n == 1)
return true;
if (n == 0)
return false;
bool a[1000] = {false}; //recording array
int res = n;
while (true)
{
int now = 0;
while (res)
{
now += (res % 10) * (res % 10);
res /= 10;
}
res = now;
if (res == 1)
return true;
if (a[res] == true) //appear the dead cycle
return false;
a[res] = true; //record it
}
}