zoukankan      html  css  js  c++  java
  • (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 }
  • 相关阅读:
    POJ 1228 Grandpa's Estate | 凸包
    POJ 2187 Beauty Contest | 旋转卡壳
    POJ 3348 Cows | 凸包模板题
    POJ 1375 Intervals | 解析几何
    POJ 2074 | 线段相交
    POJ 1039 Pipe | 线段相交
    POJ 3304 Segments | 线段相交
    POJ 2318 TOYS | 二分+判断点在多边形内
    jpg、jpeg、png... 的区别
    xhr.readyState就绪状态
  • 原文地址:https://www.cnblogs.com/cpoint/p/3023063.html
Copyright © 2011-2022 走看看