0

Here is my code:

#include <iostream>
#include <string>
#include <utility>
int main(){
    std::string swapInt, b;
    int testCase, integer;
    bool loop;
    std::cin>>testCase;
    while(testCase--){
        std::cin>>integer;
        int a = integer;
        integer++;
        loop = true;
        if(a >= 1000000 || a<=0){
            std::cout<<"Error, number too extreme\n"<<std::endl;
            loop = false;
            testCase++;
        }
        while(loop){
            std::string temp = std::to_string(integer);
            b = temp;
            swapInt = b;
            for(long long unsigned int i=0;i<b.size()/2;i++){
                std::swap(swapInt[i], swapInt[swapInt.size()-i-1]);
            }
            if(swapInt==b){
                loop = false;
                break;
            }else{
                int temp2 = std::stoi(b);
                temp2++;
                integer = temp2;
            }
        }
        if(a >= 1000000 || a<= 0){
            continue;
        }else{
            std::cout<<"Nearest Palindrome is: "<<b<<std::endl;
        }
    }
    return 0;
}

It has appeared to me that this code met all the criteria, though the runtime error has still occurred

Here is the link to the question: https://www.spoj.com/problems/PALIN/

Log
  • 1
  • 3
  • You may want to lower your expectations, 10000000000000000000 seems too big to fit in an `int`. See e.g. https://stackoverflow.com/questions/17928865/correct-way-to-use-cin-fail and https://stackoverflow.com/questions/39282953/how-to-reset-stdcin-when-using-it – Bob__ May 06 '23 at 23:21
  • spoj ran the code and just show me SIGXFSZ error for some apparent reason – Log May 07 '23 at 07:41
  • Are you trying to solve [PALIN](https://www.spoj.com/problems/PALIN/)? Note that the text says "For a given positive integer K of not more than 1000000 *digits*". Use strings, there are no built-in integral types in C++ capable of managing numbers so big. – Bob__ May 07 '23 at 07:53
  • I did tried to solve it, I dont decide the input, I am just trying to find why I keep getting errors like SIGXFSZ or wrong answer even if it functioned perfectly as a code. – Log May 07 '23 at 08:07
  • Again, `int integer; std::cin>>integer;` will try to read in an [`int`](https://en.cppreference.com/w/cpp/types/numeric_limits/max) and [*fail*](https://godbolt.org/z/M81se51n8), if you input something like 10000000000000000000. That value won't be removed from the stream, so that the extraction will continue to fail (producing the [SIGXFSZ](https://discuss.codechef.com/t/sigxfsz-runtime-error/2828)). So, read in a string, from the beginning. – Bob__ May 07 '23 at 08:23

0 Answers0