fork(6) download
  1. #include <iostream>
  2. using namespace std;
  3. void increment(long long index,long long right,long long left);
  4. bool larger(long long left,long long right);
  5.  
  6.  
  7. string k;
  8. long long length;
  9.  
  10. void inc(long long index)
  11. {
  12.  
  13.  
  14. while(k[index]=='9' && index >=0)
  15. {
  16. k[index]=0;
  17. index--;
  18. }
  19.  
  20. if(index!=-1)
  21. k[index]=char(k[index]+1);
  22. else
  23. {
  24.  
  25. length++;
  26. k.resize(length);
  27. k[0]='1';
  28. for(int i=1;i<length;i++)
  29. {
  30. k[i]='0';
  31. }
  32. }
  33.  
  34.  
  35.  
  36. }
  37.  
  38. int main() {
  39.  
  40. bool l;
  41. long long left;
  42. long long right,i,j;
  43.  
  44. int t;
  45. cin>>t;
  46. while(t--)
  47. {
  48. cin>>k;
  49. length=k.length();
  50. inc(length-1);
  51. left=length/2 -1;
  52. if(length & 1)
  53. {
  54. right=length/2 +1;
  55. l=larger(left,right);
  56. if(l)
  57. {
  58. for(i=right,j=left;i<length;i++,j--)
  59. {
  60. k[i]=k[j];
  61. }
  62. cout<<k<<endl;
  63. }
  64. else
  65. {
  66. increment(left+1,right,left);
  67. }
  68. }
  69. else
  70. {
  71. right=length/2;
  72. l=larger(left,right);
  73.  
  74. if(l)
  75. {
  76. for(i=right,j=left;i<length;i++,j--)
  77. {
  78. k[i]=k[j];
  79. }
  80. cout<<k<<endl;
  81. }
  82. else
  83. {
  84. increment(left,right,left);
  85. }
  86. }
  87. }
  88. return 0;
  89. }
  90.  
  91. bool larger(long long left,long long right)
  92. {
  93.  
  94. while(left>=0)
  95. {
  96.  
  97. if(k[left]==k[right])
  98. {
  99. if(left==0)
  100. return true;
  101.  
  102. left--;
  103. right++;
  104.  
  105.  
  106. }
  107. else if(k[left]>k[right])
  108. {
  109.  
  110. return true;
  111. }
  112. else
  113. {
  114.  
  115. return false;
  116. }
  117. }
  118.  
  119. }
  120.  
  121. void increment(long long index,long long right,long long left)
  122. {
  123.  
  124. long long i,j,in;
  125. in=index;
  126.  
  127. while(k[index]=='9' && index >=0)
  128. {
  129. k[index]='0';
  130. index--;
  131. }
  132. if(index!=-1)
  133. {
  134. k[index]=char(k[index]+1);
  135.  
  136. for(i=right,j=left;i<length;i++,j--)
  137. {
  138. k[i]=k[j];
  139. }
  140.  
  141. cout<<k<<endl;
  142. }
  143. /* will never reach this portion
  144.   else
  145.   {
  146.   }
  147.   */
  148. }
Success #stdin #stdout 0s 2824KB
stdin
1
12199121
stdout
12200221