0

I'm trying a problem on SPOJ ("Sphere Online Judge", a programming puzzle site), where I need to generate the smallest palindrome larger than the given number. I tried to solve it by calculating two sides of palindrome with modulo and int division, but it still gives me the wrong answer. What can be the problem?

#include <cmath>
#include <iostream>

using namespace std;

int inverse(int a){
    int inv = 0;
    while( a > 0){
        inv = inv*10 + a%10;
        a = a/10;
    } // while
    return inv;
} // inverse

int main(){
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        int a;
        cin >> a;
        int size  = 0;
        int tmp = a;
        while(tmp > 0){
            size++;
            tmp/=10;
        } // while

        bool even = false;
        int middle = size/2;
        if(size%2==0)even = true;
        if(!even)middle++;
        int l = a/pow(10.0,size-middle);
        int r = a%int(pow(10.0,middle));
        int lr = inverse(l);
        if(lr <= r){
            l++;
            lr=inverse(l);
        } // if

        if(!even)
            lr%=int(pow(10.0,middle-1));

        int wynik = l*pow(10.0,size-middle)+lr;

        if(a==9)
            wynik=11;
        else if(a==0)
            wynik = 1;

        cout << wynik << endl;
    } // for

    return 0;
} // main
bartexsz
  • 239
  • 1
  • 11
  • What is SPOJ? more characters – Almo Aug 09 '12 at 15:40
  • @Almo: [tag:spoj]: *"The Sphere Online Judge (http://www.spoj.pl/) is a site with a collection of programming problems of varying difficulty.*" – Zeta Aug 09 '12 at 15:41
  • What input did you give it, what output did you expect, and what output did you actually receive? – Kevin Aug 09 '12 at 16:58
  • @Kevin: Actually, for every input i give(ex: 112, 99), result is ok(121, 101), but when I submit it to SPOJ,it tells "Wrong Answer", so there must be a case for which my program returns wrong output. – bartexsz Aug 09 '12 at 17:31
  • I tested your algorithm against my own implementation, and the results are the same up to 1,000,000. So I think your math is sound but you're somehow printing it wrong. Maybe you shouldn't print an `endl` after the last output? – Kevin Aug 09 '12 at 17:38
  • Unfortunately, I can't do that by calculations. My output is good, but I have read about input again, and found out that input number can have 1,000,000 digits, so I have to solve this problem with strings. Anyway, thanks for help. – bartexsz Aug 09 '12 at 17:57
  • How are you gonna handle a integer with 1 mil digits? – Thang Do Aug 22 '12 at 09:52

1 Answers1

0

According to the problem description, you have to be able to handle positive integers of any size less than one million digits. Your implementation is based on the int type, so it can only handle numbers up to around nine digits long (give or take a few digits depending on your system's INT_MAX).

Sample input:

1
100000000000000000000000000000000000000000

Sample output:

-2147483648

Your output is incorrect. The correct output is 100000000000000000000000000000000000000001.

Kevin
  • 74,910
  • 12
  • 133
  • 166