[ create a new paste ] login | about

Link: http://codepad.org/z8XwLaYo    [ raw code | fork ]

Debanjan - C, pasted on Oct 21:
/*
  Name: SPOJ Classical PALIN
  Copyright: Freeware
  Author: nthrgeek
  Date: 21/10/09 20:53
  Description: See my blog (http://nthrgeek.worpress.com)
*/

#include<stdio.h>
#define MAX 1000001

int main()
{

  char Input[MAX];
  int t,i,
      InputLength,
      Increment,
      Carry;

  for(scanf("%d%*c",&t);t>0;t--)
  {
    int c;
    for(InputLength = 0;((c = getchar())!= '\n');)
        Input[InputLength++] = c;

	
	for(Increment = 1,i=(InputLength-2)/2;i>=0;i--)
       {
           if(Input[i]>Input[InputLength-i-1])
		     {
               Increment = 0;
               break;
             }

           if(Input[i]<Input[InputLength-i-1])break;
       }

       if(!Increment){
         for(i=0;i<InputLength/2;i++) putchar(Input[i]);

         if(0 != (InputLength%2)) putchar (Input[InputLength/2]);

         for(i=InputLength/2-1;i>=0;i--) putchar(Input[i]);
       }

       else{
             for(i=(InputLength-1)/2;i>=0;i--)
                if(Input[i] != '9') break;

              if(i<0){
                  putchar('1');
                  for(i=1;i<InputLength;i++) putchar('0');
                  putchar('1');
              }

              else{
                  for(i=(InputLength-1)/2,Carry = 1;Carry;i--){
                      if(Input[i] == '9') Input[i] = '0';
                      else {Input[i]++;Carry--;}
                  }

                  for(i=0;i<InputLength/2;i++) putchar(Input[i]);

                  if(InputLength%2) putchar(Input[InputLength/2]);

                  for(i=InputLength/2-1;i>=0;i--) putchar(Input[i]);
              }
       }
       putchar('\n');
  }

  return 0;
}


Create a new paste based on this one


Comments: