(HDU)1282 -- 回文数猜想

题目链接:http://vjudge.net/problem/HDU-1282

做这个题目的时候想了很多。

第一,怎么判断一个数是不是回文数(可以用int表示的时候):

最初的想法是用字符串处理,int(log10()+1)来得到位数...(服了自己)...

然后从两段向中间对比,全部满足就是回文数,后来觉得好麻烦啊。

后来写了个函数得出一个数的反过来的数字,要用的时候用个if语句判断。

第二,格式问题...

反正做这题的时候脑子有点懵,状态很一般,打不过水题。

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <string>
 7 #include <cstdlib>
 8 
 9 using namespace std;
10 
11 int f(int n)
12 {
13     int nf=0;
14     while(n)
15     {
16         nf=nf*10+n%10;
17         n/=10;
18     }
19     return nf;
20 }
21 
22 int main()
23 {
24     int n,i,num[100];
25     while(~scanf("%d",&n))
26     {
27         int cnt=0;
28         if (n==f(n)) printf("0\n%d\n",n);
29         num[0]=n;
30         while(n!=f(n))
31         {
32             cnt++;
33             n+=f(n);
34             num[cnt]=n;
35         }
36         if(cnt)
37         {
38             printf("%d\n",cnt);
39             printf("%d",num[0]);
40             for(i=1;i<=cnt;i++)
41                 printf("--->%d",num[i]);
42             printf("\n");
43         }
44     }
45     return 0;
46 }

 

posted @ 2016-12-05 19:48  ACDoge  阅读(423)  评论(0编辑  收藏  举报