閱讀801 返回首頁    go 阿裏雲 go 技術社區[雲棲]


uva 10591 - Happy Number hash

   非常簡單的題,易知平方和後不超過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");
    }
}



最後更新:2017-04-04 07:03:44

  上一篇:go UbuntuEclipse顏色提示修改(WinXP窗口背景顏色修改)+快捷方式啟動
  下一篇:go CF27.E. Number With The Given Amount Of Divisors