PALIN - The Next Palindrome 对称的数

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

转载于:https://www.cnblogs.com/geekvan/p/6239220.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值