非常简单的题,易知平方和后不超过800,开个800的数组,预处理,然后直接定址就可以了,不过要注意a和an的区别
/* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue> #define INF 1E9 using namespace std; int is[800]; int solve(int now) { if(now<800&&is[now]!=-1)return is[now]; int t=now,ans=0; while(t) { ans+=(t%10)*(t%10); t/=10; } if(now<800)is[now]=0; t=solve(ans); if(now<800)is[now]=t; return t; } int main() { int i,C=0,T,t; memset(is,-1,sizeof(is)); is[1]=1; for(i=2;i<800;i++) solve(i); scanf("%d",&T); while(T--) { scanf("%d",&t); printf("Case #%d: %d is %s number.\n",++C,t,solve(t)?"a Happy":"an Unhappy"); } }