Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- int main() {
- int test_cases = 0, i = 0, mid = 0, len = 0, left = 0, right = 0, j, check_9, u_digit;
- char input[1000002];
- scanf("%d", &test_cases);
- while(test_cases > 0) {
- scanf("%s", input);
- len = strlen(input);
- mid = len/2;
- check_9 = 0;
- u_digit = 0;
- for(i=0;i<len;i++) { //all digits are 9
- if(input[i] != '9')
- check_9 = 1;
- }
- if(!check_9) {
- for(i=1;i<len;i++)
- input[i] = '0';
- input[0] = input[len] = '1';
- input[len+1] = '\0';
- }
- else {
- if(len == 1) {
- input[0] = ((input[0] - '0') + 1) + '0';
- }
- else if(len%2 == 0) {
- for(left=mid - 1,right=mid; left>=0,right<len; left--,right++) {
- if(input[left] > input[right]) {
- input[right] = input[left];
- u_digit = 1;
- }
- else if(input[left] == input[right]) {
- if(left!=0 && right!=len - 1)
- input[right] = input[left];
- else { //in case a palindrome
- goto even_shift;
- }
- }
- else {
- even_shift: if(u_digit) {
- input[right] = input[left];
- }
- else {
- if(input[mid - 1] != '9' ) {
- input[mid - 1] = ((input[mid - 1] - '0') + 1) + '0';
- }
- else {
- j = mid - 1;
- while(input[j] == 9 && (j-1)<=0) {
- input[j] = '0'; input[j-1] = ((input[j - 1] - '0') + 1) + '0';
- j--;
- }
- }
- for(left=mid - 1,right=mid; left>=0,right<len; left--,right++)
- input[right] = input[left];
- break;
- }
- }
- }
- }
- else {
- for(left=mid - 1,right=mid+1; left>=0,right<len; left--,right++) {
- if(input[left] > input[right]) {
- input[right] = input[left];
- u_digit = 1;
- }
- else if(input[left] == input[right]) {
- if(left!=0 && right!=len - 1)
- input[right] = input[left];
- else { //in case a palindrome
- goto odd_shift;
- }
- }
- else {
- odd_shift: if(u_digit) {
- input[right] = input[left];
- }
- else {
- if(input[mid] != '9')
- input[mid] = ((input[mid] - '0') + 1) + '0';
- else {
- j = mid;
- while(input[j] == 9 && (j-1)<=0) {
- input[j] = '0'; input[j-1] = ((input[j - 1] - '0') + 1) + '0';
- j--;
- }
- }
- for(left=mid - 1,right=mid+1; left>=0,right<len; left--,right++)
- input[right] = input[left];
- break;
- }
- }
- }
- }
- }
- printf("%s\n", input);
- test_cases --;
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement