Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main() {
  5.  
  6.         int test_cases = 0, i = 0, mid = 0, len = 0, left = 0, right = 0, j, check_9, u_digit;
  7.         char input[1000002];
  8.  
  9.         scanf("%d", &test_cases);
  10.        
  11.         while(test_cases > 0) {
  12.                 scanf("%s", input);
  13.                 len = strlen(input);
  14.  
  15.                 mid = len/2;
  16.                 check_9 = 0;
  17.                 u_digit = 0;
  18.                 for(i=0;i<len;i++) { //all digits are 9
  19.                         if(input[i] != '9')
  20.                                 check_9 = 1;
  21.                 }
  22.  
  23.                 if(!check_9) {
  24.                         for(i=1;i<len;i++)
  25.                             input[i] = '0';
  26.                         input[0] = input[len] = '1';
  27.                         input[len+1] = '\0';
  28.                 }
  29.  
  30.                 else {
  31.                         if(len == 1) {
  32.                                 input[0] = ((input[0] - '0') + 1) + '0';
  33.                 }
  34.                     else if(len%2 == 0) {
  35.                             for(left=mid - 1,right=mid; left>=0,right<len; left--,right++) {
  36.                                 if(input[left] > input[right]) {
  37.                                         input[right] = input[left];
  38.                                         u_digit = 1;
  39.                                     }
  40.                                     else if(input[left] == input[right]) {
  41.                                             if(left!=0 && right!=len - 1)
  42.                                                 input[right] = input[left];
  43.                                             else { //in case a palindrome
  44.                                                 goto even_shift;
  45.  
  46.                                         }
  47.                                     }
  48.                                     else {
  49. even_shift:                     if(u_digit) {
  50.                                                 input[right] = input[left];
  51.                                         }
  52.                                         else {
  53.                                                 if(input[mid - 1] != '9' ) {
  54.                                                         input[mid - 1] = ((input[mid - 1] - '0') + 1) + '0';
  55.                                                 }
  56.                                                 else {
  57.                                                         j = mid - 1;
  58.                                                         while(input[j] == 9 && (j-1)<=0) {
  59.                                                                 input[j] = '0'; input[j-1] = ((input[j - 1] - '0') + 1) + '0';
  60.                                                                 j--;
  61.                                                         }
  62.                                                 }
  63.                                                 for(left=mid - 1,right=mid; left>=0,right<len; left--,right++)
  64.                                                         input[right] = input[left];
  65.                                                 break;
  66.                                         }
  67.                        
  68.                                 }
  69.                         }
  70.                        
  71.                 }
  72.                 else {
  73.                         for(left=mid - 1,right=mid+1; left>=0,right<len; left--,right++) {
  74.                                 if(input[left] > input[right]) {
  75.                                         input[right] = input[left];
  76.                                         u_digit = 1;
  77.                                 }
  78.                                 else if(input[left] == input[right]) {
  79.                                         if(left!=0 && right!=len - 1)
  80.                                                 input[right] = input[left];
  81.                                         else { //in case a palindrome
  82.                                                 goto odd_shift;
  83.                                         }
  84.                                 }
  85.                                 else {
  86. odd_shift:                      if(u_digit) {
  87.                                                 input[right] = input[left];
  88.                                         }
  89.                                         else {
  90.                                                 if(input[mid] != '9')
  91.                                                         input[mid] = ((input[mid] - '0') + 1) + '0';
  92.                                                         else {
  93.                                                             j = mid;
  94.                                                            while(input[j] == 9 && (j-1)<=0) {
  95.                                                                 input[j] = '0'; input[j-1] = ((input[j - 1] - '0') + 1) + '0';
  96.                                                                 j--;
  97.                                                         }
  98.                                                 }
  99.                                                 for(left=mid - 1,right=mid+1; left>=0,right<len; left--,right++)
  100.                                                                 input[right] = input[left];
  101.                                                 break;
  102.                                         }
  103.                                     }
  104.                             }
  105.                     }
  106.                 }
  107.                 printf("%s\n", input);
  108.                 test_cases --;
  109.         }
  110. return;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement