简体   繁体   中英

I am solving the problem of next palindrome but when I submit it gives me sigsegv error

I tried to run the code on my IDE with all possible exception testcases and it's working fine, but I can't really figure out why it is showing runtime error(SIGSEGV). For your reference,I am providing you the link of the problem:- https://www.spoj.com/problems/PALIN/

#include <iostream>
#include <vector>
using namespace std;

bool check_nines(vector<long long> &num, long long size)
{
    long long count = 0;
    for (long long i = 0; i < size; i++)
    {
        if (num[i] == 9)
            count++;
    }
    if (count == size)
        return true;
    else
        return false;
}

void copy(vector<long long> &v, long long low, long long high)
{
    for (long long i = low; i >= 0; i--)
    {
        v[high] = v[i];
        high++;
    }
}

void next_num(vector<long long> &v, long long low, long long high)
{
    if (v[low] > v[high])
    {
        copy(v, low, high);
    }
    else if (v[low] < v[high])
    {
        v[low] += 1;
        copy(v, low, high);
    }
    else
    {
        long long temp_low = low;
        long long temp_high = high;
        while (temp_low != 0 && temp_high != v.size() - 1)
        {
            if (v[temp_low] != v[temp_high])
                break;
            temp_high++;
            temp_low--;
        }
        if (v[temp_low] > v[temp_high])
        {
            copy(v, low, high);
        }
        else
        {
            v[low] += 1;
            copy(v, low, high);
        }
    }
}

void next_num2(vector<long long> &v, long long low, long long high, long long mid)
{
    if (v[low] > v[high])
    {
        copy(v, low, high);
    }
    else if (v[low] < v[high])
    {
        if (v[mid] == 9)
        {
            v[mid] = 0;
            v[low] += 1;
            copy(v, low, high);
        }
        else
        {
            v[mid] += 1;
            copy(v, low, high);
        }
    }
    else
    {
        long long temp_low = low;
        long long temp_high = high;
        while (temp_low != 0 && temp_high != v.size() - 1)
        {
            if (v[temp_low] != v[temp_high])
                break;
            temp_high++;
            temp_low--;
        }
        if (v[temp_low] > v[temp_high])
        {
            copy(v, low, high);
        }
        else
        {
            if (v[mid] == 9)
            {
                v[mid] = 0;
                v[low] += 1;
                copy(v, low, high);
            }
            else
            {
                v[mid] += 1;
                copy(v, low, high);
            }
        }
    }
}

void find_next_palindrome(vector<long long> &v, long long size)
{
    if (check_nines(v, size))
    {
        v[0] = 1;
        v.push_back(1);
        for (long long i = 1; i < size; i++)
            v[i] = 0;
        
    }
    else if (size % 2 == 0)
    {
        long long mid = size / 2;
        long long low = mid - 1;
        long long high = mid;
        next_num(v, low, high);
        
    }
    else if (size % 2 != 0)
    {
        long long mid = size / 2;
        long long low = mid - 1;
        long long high = mid + 1;
        next_num2(v, low, high, mid);
    }
    cout << "\n";
}

int main()
{
    long long t;
    cin >> t;
    while (t--)
    {
        long long a, count = 0, temp;
        cin >> a;
        temp = a;
        while (temp)
        {
            count++;
            temp /= 10;
        }
        vector<long long> v(count);
        while (a)
        {
            v[count - 1] = a % 10;
            a /= 10;
            count--;
        }
        long long size = v.size();
        if(size==1 && v[0]!=9)
            cout<<v[0]+1;
        else{
            find_next_palindrome(v, size);
            for (long long i = 0; i < v.size(); i++)
                cout << v[i];
        }
        cout << "\n";
    }
    return 0;
}

The base case Cero returns SIGSEGV

But also, I think you should first save all the input numbers and after that calculate and show the results

For example:

4

123 156 42 99

131 161 44 101

Your code should looks like:

int main()
{
    std::vector<long long> input;
    long long t;
    cin >> t;

    for (int i = 0; i < t; i++)
    {
        long long temp;
        cin >> temp;
        input.push_back(temp);
    }
    
    for(t=0; t<input.size();t++)
    {
        long long a, count = 0, temp;
        a = input[t];
        temp = a;

        if (a < 9)
        {
            cout << a;
            cout << "\n";
            continue;
        }

        while (temp)
        {
            count++;
            temp /= 10;
        }
        vector<long long> v(count);
        while (a)
        {
            v[count - 1] = a % 10;
            a /= 10;
            count--;
        }
        long long size = v.size();
        find_next_palindrome(v, size);
        for (long long i = 0; i < v.size(); i++)
            cout << v[i];
        
        cout << "\n";
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM