回文数 (Palindrome Numbers,Dhaka 2003,LA 2889)

 1 #include <iostream>
 2 #include <string.h>
 3 #include <string>
 4 #include <fstream>
 5 #include <algorithm>
 6 #include <stdio.h>
 7 #include <vector>
 8 #include <queue>
 9 #include <set>
10 #include <cmath>
11 using namespace std;
12 const double eps = 1e-8;
13 const int INF=0x7fffffff;
14 unsigned long long uINF = ~0LL;
15 #define MAXN 10000007
16 typedef long long LL;
17 LL vis[MAXN];
18 LL prime[MAXN];
19 
20 void sieve(LL n)
21 {
22     LL m=(LL)sqrt(n+0.5);
23     memset(vis,0,sizeof(vis));
24     for(LL i=2;i<=m;i++)if(!vis[i])
25     for(LL j=i*i;j<=n;j+=i)vis[j]=1;
26 }
27 
28 LL gen_prime(LL n)
29 {
30     sieve(n);
31     LL c=0;
32     for(LL i=2;i<=n;i++)if(!vis[i])
33         prime[c++]=i;
34     return c;
35 }
36 
37 LL gcd(LL a,LL b)
38 {
39     return b==0?a:gcd(b,a%b);
40 }
41 LL num[11];
42 LL ex[11];
43 void init()
44 {
45     num[0]=1;ex[0]=0;
46     num[1]=9;ex[1]=1;
47     for(int i=2;i<=10;i++)
48     {num[i]=num[i-1]*10;
49     ex[i]=ex[i-1]*10;}
50 
51     //for(int i=1;i<11;i++)
52    //cout<<num[i]<<' ';
53 }
54 
55 string Reverse(string str1)
56 {
57     string str2="";
58     for(int i=str1.length()-1;i>=0;i--)
59     str2+=str1[i];
60     return str2;
61 }
62 
63 int main()
64 {
65     LL n;
66     init();
67     while(scanf("%lld",&n),n)
68     {
69         int i;
70         string ans="";
71         bool odds;
72         LL dig=1;
73         for(i=1;i<11;i++)
74         {
75             if(n>num[i])n-=num[i];
76             else {odds=true;break;}
77             if(n>num[i])n-=num[i];
78             else {odds=false;break;}
79             dig*=10;
80         }
81         n--;n+=dig;
82         while(n>0)
83         {
84             ans+=n%10+'0';
85             n/=10;
86         }
87         ans=Reverse(ans);
88         if(odds)ans+=Reverse(ans.substr(0,ans.length()-1));
89         else ans+=Reverse(ans);
90         cout<<ans<<endl;
91     }
92 
93     return 0;
94 }

 

posted @ 2013-07-22 18:44  TO_Asia  阅读(264)  评论(0编辑  收藏  举报