首页 > 其他 > 详细

PALIN - The Next Palindrome 对称的数

时间:2016-12-31 15:29:03      阅读:197      评论:0      收藏:0      [点我收藏+]

A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.

Input

The first line contains integer t, the number of test cases. Integers K are given in the next t lines.

Output

For each K, output the smallest palindrome larger than K.

Example

技术分享

 

 1 #include<iostream>
 2 #include<string>
 3 #define MAX 7
 4 using namespace std;
 5 
 6 int arr[MAX];
 7 int ptr;
 8 string ss="";
 9 void pushstack(int t)
10 {
11     arr[ptr]=t;
12     ptr++;
13 }
14 
15 int  popstack()
16 {
17     ptr--;
18     return arr[ptr];
19 }
20 
21 void Palindrome(int m)
22 {
23     m=m+1;
24     int i,j;
25 
26    while(1)
27    {
28        i=m;
29        j=m;
30        ptr=0;
31        bool tag=false;//标记
32     while(i>0)
33     {
34         int k;
35         k=i%10;
36         pushstack(k);
37         i=i/10;
38     }
39     while(j>0)
40     {
41         int k;
42         k=j%10;
43         int f=popstack();
44         if(k!=f)
45             break;
46         else if(k==f&&ptr==0)
47         {
48             tag=true;
49             char s[MAX];
50             sprintf(s,"%d\n",m);
51             ss=ss+s;
52         }
53         j=j/10;
54     }
55     if(tag==true)
56         break;
57     m=m+1;
58    }
59 
60 }
61 
62 int main()
63 {
64     int i;
65     cin>>i;
66     while(i>0)
67     {
68         int j;
69         cin>>j;
70         i--;
71         Palindrome(j);
72     }
73     cout<<ss<<endl;
74     return 0;
75 }

代码截图:

技术分享

2016-12-3114:24:08 By--LeeVanVan

PALIN - The Next Palindrome 对称的数

原文:http://www.cnblogs.com/geekvan/p/6239220.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!