(SPOJ5)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

Input:
2
808
2133

Output:
818
2222

算法:

由于数据较大,只能采用字符数组存储输入字符串

(1)判断字符串s是否是全9字符串“9999....99”,若是,直接打印“1(len(s)-1个0)1”

(2)将字符串均分为2个部分,中心位置为p,若len为奇数,p=len/2;若len为偶数,p=len/2-1;

(3)若前半部分的所有字符s[i]都大于后半部分的相应字符s[len-i-1],直接将前半部分的字符复制到后半部分的相应位置;

(4)若前半部分的字符至少有一个字符小于后半部分的相应字符或是字符串本身就是回文数,则将中间位置的字符p+=1,若有进位,依次向前进位,直到 i=0,最后将前半部分的字符复制到后半部分的相应位置;

代码如下:

  1 #include <stdio.h>
  2 #include <math.h>
  3 #include <string.h>
  4 #include <ctype.h>
  5 
  6 char a[1000001];
  7 
  8 void deal(char *s)
  9 {
 10     int i,j,len,flag1,flag2,temp,flag,sum,L;
 11     flag1=flag2=flag=1;
 12     sum=0;
 13     len=strlen(s);
 14     L=len/2-1; 
 15     for(i=0; i<len; i++)
 16     {
 17         if(s[i]!='9')
 18         {
 19           flag1=0;  //判断s[i]是否都为'9' 
 20           break;
 21         }
 22     }
 23     for(i=0; i<=L; i++)
 24     {
 25         if(s[i]<s[len-1-i])
 26         {
 27             flag2=0;
 28         }
 29         else if(s[i]==s[len-1-i])
 30         {
 31             sum++;
 32         }
 33         s[len-1-i]=s[i];
 34     }
 35     if(sum==len/2)
 36     {
 37         flag2=0;
 38     }  
 39     if(len==1)
 40     {
 41         printf("11\n");
 42         return;
 43     }
 44     else
 45     {
 46         if(flag1)   //s为999....999 
 47         {
 48             printf("1");
 49             for(j=0; j<len-1; j++)
 50              printf("0");
 51             printf("1\n");
 52             return;
 53         }
 54         else
 55         {
 56           if(flag2)
 57           {
 58             printf("%s\n",s);
 59           }
 60           else
 61           {
 62               if(len%2)
 63               {
 64                 i=len/2;
 65               }
 66               else
 67               {
 68                   i=len/2-1;
 69               }
 70               while(i>-1)
 71               {    
 72                   if(s[i]=='9')
 73                   {
 74                       s[i]='0';
 75                   }
 76                   else
 77                   {
 78                     s[i]=s[i]+1;
 79                   break;    
 80                   }
 81                   i--;
 82               }
 83               for(i=0; i<len/2; i++)
 84               {
 85                  s[len-1-i]=s[i];
 86               }
 87               printf("%s\n",s);
 88           }
 89     
 90         }
 91     }
 92     
 93 }
 94 
 95 void solve()
 96 {
 97     int t,n;
 98     scanf("%d",&t);
 99     getchar();
100     while(t--)
101     {
102         gets(a);
103         deal(a);
104     }
105 }
106 
107 int main()
108 {
109   solve();
110   return 0;
111 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值