fork download
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. int main()
  8. {
  9. int t , i; // t = number of test cases , i = a temporary variable for iteration purpose
  10. char *input , *output ;
  11. unsigned long long int l , x , y , mid ; // l = length of input string , x = to iterate from mid to start ,
  12. // y = to iterate from mid to end
  13. // mid = middle location of input string (even : l/2-1 , odd : l/2)
  14. scanf("%d",&t);
  15.  
  16. while (t--)
  17. {
  18. input = (char *)malloc(1000000);
  19. scanf("%s",input);
  20. l = strlen(input);
  21.  
  22. //check if input is single digit
  23. if(l==1)
  24. {
  25. printf("11\n");
  26. break;
  27. }
  28. //check if input is already a palindrome of kind '99','99','9999',etc. so that output becomes : '101' , '10001'
  29. i = 0 ;
  30. for(i = 0 ; i< l ; i++)
  31. {
  32. if(input[i]!='9')
  33. break;
  34. }
  35.  
  36. if(i==l)
  37. {
  38. output = malloc(l+2);
  39. output[0]='1';
  40. output[l]='1';
  41. for(i=1;i<l;i++)
  42. output[i]='0';
  43. output[l+1]='\0';
  44. printf("%s\n",output);
  45. break;
  46. }
  47.  
  48.  
  49. //make a string containig all digits of input
  50. output = malloc(l);
  51. strcpy(output,input);
  52.  
  53. //find the mid number
  54. l = strlen(output);
  55. output[l-1]=output[l-1]+1;
  56. if (l%2==0)
  57. {
  58. //even
  59. mid = l/2 ;
  60. x = mid - 1 ;
  61. y = mid ;
  62.  
  63. //scan until the last the element , and if break appears then start copying the remaining elements
  64. // as shown in [1]
  65. while (y!=l)
  66. {
  67. //check if first half element is less than second half element ,
  68. // if yes , it means the first half element needs to be increased by 1 and second half //element is made equal to first half element, Ex : 2473 : 2553
  69. //condition(1)
  70. if (output[x] < output[y])
  71. {
  72. output[x]=output[x]+1 ;
  73. output[y]=output[x];
  74. break ;
  75.  
  76. }
  77. //if the two elements are already equal as in '236679' , '6' == '6' , then we check for
  78. // '3' and '7'
  79. //condition(2)
  80. else if (output[x] == output[y] && output[x] != '\0')
  81. {
  82. x--;
  83. y++;
  84. if(output[x] < output[y]) //if less , then we do the same as condition(1)
  85. {
  86. output[x+1] = output[x+1]+1;
  87. output[y-1] = output[y-1]+1;
  88. }
  89. //no break , so that we can check for next elements
  90. }
  91. //special case for handling 9s
  92. else if (output[x] == '9' && output[y]== '9')
  93. {
  94. while (output[x]== output[y]) //case like '199999'
  95. {
  96. x--;
  97. y++;
  98.  
  99. }
  100.  
  101. if (output[x] < output[y]) //same as condition(1)
  102. {
  103. output[x]=output[x]+1;
  104. output[y]=output[x];
  105. for (i=x+1; i<=y-1; i++) // but also replace the 9s with 0
  106. output[i]='0';
  107. }
  108. break;
  109. }
  110. //condition(4)
  111. else if (output[x] > output[y]) //if first half element is greater than //second half element , then simply replace the second half element with first half element
  112. {
  113. output[y]=output[x];
  114. break ;
  115. }
  116.  
  117. x--;
  118. y++;
  119. }
  120. //start copying unscanned elements of first half elements to second half elements
  121. while (y <= l-1 )
  122. {
  123. output[y]=output[x];
  124. x--;
  125. y++;
  126. }
  127.  
  128. }
  129. else
  130. {
  131. //odd
  132. mid = l/2 ;
  133. x = mid - 1 ;
  134. y = mid + 1;
  135.  
  136. while (y!=l)
  137. {
  138. if (output[x] <= output[y] && output[mid] != '9')
  139. {
  140. output[mid]=output[mid]+1;
  141. break ;
  142. }
  143. else if (output[x] <= output[y] && output[mid] == '9')
  144. {
  145. while (output[x] == output[y])
  146. {
  147. x--;
  148. y++;
  149.  
  150. }
  151.  
  152. if (output[x] < output[y]) // 14897 case
  153. {
  154. output[x]=output[x]+1;
  155. output[y]=output[x];
  156. for (i=x+1; i<=y-1; i++)
  157. output[i]='0';
  158. }
  159. break;
  160. }
  161. else if (output[x] > output[y])
  162. {
  163. output[y]=output[x];
  164. break ;
  165. }
  166.  
  167. x--;
  168. y++;
  169. }
  170.  
  171. while (y <= l-1 )
  172. {
  173. output[y]=output[x];
  174. x--;
  175. y++;
  176. }
  177.  
  178.  
  179.  
  180. }
  181.  
  182.  
  183.  
  184. //check
  185. x = 0 ;
  186. y = l -1 ;
  187. while(output[x]==output[y] && output[x] != '\0')
  188. {
  189. x++;
  190. y--;
  191. }
  192. if(x-1 == l-1)
  193. {
  194. printf("%s\n",output);
  195. }
  196.  
  197.  
  198.  
  199.  
  200. }
  201.  
  202. free(input);
  203. free(output);
  204.  
  205. return 0 ;
  206. }
  207.  
Success #stdin #stdout 0s 1924KB
stdin
Standard input is empty
stdout
1