题意:最大数字也就81*9,不会很大,那就简单了
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
int sort(int x)
{
int y=0;
while (x>0)
{
y=y+(x%10)*(x%10);
x=x/10;
}
return y;
}
int main()
{
int n,m,t,i,visit[1000];
scanf("%d",&t);
for (i = 1; i <= t; i++)
{
scanf("%d",&n);
memset(visit,0,sizeof(visit));
m=n;
while (n)
{
m=sort(m);
if (m==1)
{
printf("Case #%d: %d is a Happy number.\n",i,n);
break;
}
if (visit[m])
{
printf("Case #%d: %d is an Unhappy number.\n",i,n);
break;
}
visit[m]=1;
}
}
return 0;
}