在我自己的机器上工作,但在 SPOJ 上工作

Working on my own machine but SIGABRT on SPOJ

本文关键字:工作 但在 SPOJ 我自己 机器 自己的      更新时间:2023-10-16

我正在研究 SPOJ http://www.spoj.com/problems/PALIN/上的下一个回文问题。我的代码在我自己的机器上运行良好,但 SPOJ 给了我 SIGABRT。我正在使用 4.9.2 C++

"如果正整数在十进制系统中的表示从左到右和从右到左读取时相同,则称为回文。对于不超过 1000000 位的给定正整数 K,将大于 K 的最小回文值写入输出。数字始终不带前导零。

输入

第一行包含整数 t,即测试用例的数量。整数 K 在接下来的 t 行中给出。

输出

对于每个 K,输出大于 K 的最小回文。

#include<iostream>
#include<vector>
using namespace std;
// turn 9 to 10
void round(vector<int> &input,int index) {
    int len = input.size();
    input[index] = 0;
    input[len-index-1] = 0;
    // if it is the first digit, add 1 in the front
    if (index == 0) {
        input.insert(input.begin(),1);
    }
    else {
        input[index-1] ++;
        input[len-index] ++;
    }
}
// find the next palindrome
int palin(vector<int> &input) {
    int len = input.size();
    bool large = true;
    bool small = true;
    bool eqal = true;
// if it is a single digit  
    if (len == 1) {
        if (input[0] == 9) {
            input[0] = 11;
        }
        else {
            input[0] ++;
        }
        return 1;
    }
// start from the one before the middle 
    int index = len / 2 - 1;
    while (index >= 0) {
    len = input.size();
    // the number supposed to be the same as input[index]
        int rfl = len-index-1;
        // keep record for if the updated number is smaller/equal to the original
        if (input[index] > input[rfl]) {small = false; eqal = false;}
        else if (input[index] < input[rfl]) {large = false; small = true; eqal = false;}
        else {small = false;}
        if (input[index] == 10) {round(input,index);}
        else {
            input[rfl] = input[index];
        }
        index --;
    };
// restart from the one before the middle
    index = (int)input.size() / 2 - 1;
// unless all digits on the left are larger than right/the more left digits are larger but some closer to the middle are smaller or equal, increase the number
    if (!large || small || eqal) {
        len = input.size();
        if (len % 2 == 1) { // odd
            if (input[index+1] == 9) {
                round(input,index+1);
            }
            else {input[index+1] ++;}   
        }
        else { // even
            if (input[index] == 9) {
                round(input,index);
            }
            else {
                input[index-1] ++; input[index + 1] ++;
            }
    }
        // go over the digits again to make sure it is a palindrome
        while (index >= 0) {
            if (input[index] == 10) {
                round(input,index);
            }
            input[input.size()-index-1] = input[index];
            index --;
        }
    }
    return 0;
}
int main() {
    int count; // how many numbers are there
    cin >> count;
    string buffer; // temporary to store each line of input
    for (int j=0;j<count;++j) {
        vector<int> number;
        cin >> buffer;
        if (cin.fail() || buffer.size() == 0) { // not a number or length==0
            return 1;
        }
        for (int k=0;k<(int)buffer.size();k++) {
            int temp = buffer[k] - '0'; // convert ASCII to int
            number.push_back(temp); // construct vector
        }
        palin(number);
        for (int i=0;i<(int)number.size();i++) {
            cout << number[i];
        }
        cout << endl;
    }
    return 0;
}

老实说,蛮力方法效率低下,但编码起来很清楚。在这里,我只是不断迭代数字,直到我找到一个回文,对于每个数字:

http://coliru.stacked-crooked.com/a/2c7ac595d7c2cfa7

#include <iostream>
#include <stack>
#include <string>
#include <vector>
int main() {
    int count; // how many numbers are there
    std::cin >> count;
    std::string buffer;
    //two vectors to store output
    std::vector<long int> input;
    std::vector<long int> output;
    //take the inputs
    for(int i = 0; i < count; ++i) {
        std::cin >> buffer;
        input.push_back(std::stoi(buffer));
        buffer.clear();
    }
    //check that we have the inputs
    for(auto it : input) {
        std::cout << it << std::endl;
    }
    //lambda to test for palindromes
    auto is_palindrome = [](long int n) {
        auto str = std::to_string(n);
        std::stack<char> stack;
        //Load each character into the stack
        for(auto it : str) {
           stack.push(it);
        }
        //Use the property of a stack to take out in a reverse order
        for(size_t i = 0; !stack.empty(); stack.pop()) {
            if (stack.top() != str[i])
                return false;
            else    
                ++i;
        }
        return true;
    };   
    //test for the palindromes; iterate
    for(auto it : input) {
        int n;
        for (n = it+1; ; ++n) {
            if(is_palindrome(n))
                break;
            else
                continue;
        }
        output.push_back(n);
    }
    //output the outputs
    for(auto it : output) {
        std::cout << "next palindrome: " << it << 'n';   
    }
    return 0;
}
相关文章: