fork(1) download
  1. /*The next palindrome*/
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<malloc.h>
  6.  
  7. #ifndef __cplusplus
  8. #define bool unsigned
  9. #define true 1
  10. #define false 0
  11. #endif //__cplusplus
  12.  
  13. bool palin_more_than_org(const char* str,unsigned n)
  14. //str[i] gives palin, str[j] gives org
  15. {
  16. int i=n/2-1,j=(n+1)/2;
  17. for(;i>=0 && j<n;--i,++j)
  18. {
  19. if(str[j]>str[i])return false;
  20. else if(str[j]<str[i])return true;
  21. }
  22. return false;
  23. }
  24.  
  25. bool is_9s(const char* str,unsigned n)
  26. {
  27. unsigned i;
  28. for(i=0;i<n;++i)if(str[i]!='9')return false;
  29. return true;
  30. }
  31.  
  32. void next_palin(char* str,unsigned n)
  33. {
  34. unsigned i,j;
  35. if(is_9s(str,n))
  36. {
  37. str[0]='1';
  38. for(i=1;i<n;++i)str[i]='0';
  39. str[n]='1';
  40. str[n+1]='\0';
  41. return;
  42. }
  43. if(!palin_more_than_org(str,n))
  44. {
  45. //increase first half
  46. for(i=(n-1)/2;str[i]=='9';--i)str[i]='0';
  47. ++str[i];
  48. }
  49. for(i=0,j=n-1;i<j;++i,--j)str[j]=str[i];
  50. }
  51.  
  52. int main()
  53. {
  54. unsigned i,t;
  55. char* str;
  56. scanf("%u",&t);
  57. //fflush(stdin);
  58. str=(char*)malloc(10000002);
  59. for(i=0;i<t;++i)
  60. {
  61. gets(str);
  62. next_palin(str,strlen(str));
  63. puts(str);
  64. }
  65. free(str);
  66. return 0;
  67. }
Success #stdin #stdout 0s 2856KB
stdin
3
9
99
999
stdout
1
11
101