Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在我自己的机器上工作,但SIGABRT在SPOJ上工作_C++_Vector - Fatal编程技术网

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

C++ 在我自己的机器上工作,但SIGABRT在SPOJ上工作,c++,vector,C++,Vector,我正在研究SPOJ的下一个回文问题。我的代码在我自己的机器上运行得很好,但是SPOJ给了我SIGABRT。我用的是C++ 4.92 如果从左到右和从右到左读取时,正整数在十进制中的表示形式相同,则称为回文。对于给定的不超过1000000位的正整数K,将大于K的最小回文值写入输出。数字总是显示为无前导零 输入 第一行包含整数t,即测试用例的数量。整数K在接下来的t行中给出 输出 对于每个K,输出大于K的最小回文。” #包括 #包括 使用名称空间std; //9点到10点 无效圆(向量和输入,整数索

我正在研究SPOJ的下一个回文问题。我的代码在我自己的机器上运行得很好,但是SPOJ给了我SIGABRT。我用的是C++ 4.92

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

输入

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

输出

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

#包括
#包括
使用名称空间std;
//9点到10点
无效圆(向量和输入,整数索引){
int len=input.size();
输入[索引]=0;
输入[len-index-1]=0;
//如果是第一位数字,则在前面加1
如果(索引==0){
input.insert(input.begin(),1);
}
否则{
输入[索引-1]++;
输入[透镜索引]+;
}
}
//查找下一个回文
int-palin(向量和输入){
int len=input.size();
布尔大=真;
布尔小=真;
bool eqal=真;
//如果是个位数
如果(len==1){
如果(输入[0]==9){
输入[0]=11;
}
否则{
输入[0]++;
}
返回1;
}
//从中间的前一个开始
整数指数=len/2-1;
而(索引>=0){
len=input.size();
//应该与输入[索引]相同的数字
int rfl=len-index-1;
//记录更新后的数字是否小于/等于原始数字
if(input[index]>input[rfl]){small=false;eqal=false;}
else if(input[index]=0){
如果(输入[索引]==10){
四舍五入(输入、索引);
}
输入[input.size()-index-1]=输入[index];
索引--;
}
}
返回0;
}
int main(){
int count;//有多少个数字
cin>>计数;
字符串缓冲区;//临时存储每行输入
对于(int j=0;j>buffer;
如果(cin.fail()| | buffer.size()==0){//不是数字或长度==0
返回1;
}

老实说,对于(int k=0;k而言,蛮力方法效率低下,但对代码来说却非常清晰。在这里,我只是不断迭代数字,直到找到每个数字的回文:

#包括
#包括
#包括
#包括
int main(){
int count;//有多少个数字
标准::cin>>计数;
字符串缓冲区;
//存储输出的两个向量
向量输入;
std::矢量输出;
//接受输入
对于(int i=0;i>缓冲区;
输入。推回(标准::stoi(缓冲区));
buffer.clear();
}
//检查我们是否有输入
用于(自动it:输入){

std::没时间学习如何使用调试器。@船长很可能他不会,因为他不知道SPOJ输入到他的程序中的是什么导致
SIGABRT
。你的代码并不是我读过的最差的代码,但它很密集,很难阅读,而且绝对不清晰或无法自我记录,这就是为什么你在这是行不通的。你帮助自己的第一步是先编写可读的、注释过的代码,然后再进行优化(我怀疑你的代码中有一些东西是优化的,但实际上只是让它更难阅读/调试)你节省的打字时间比你失去的无法阅读的时间都要少。也就是说:
cin>>buffer
不产生数字或是空字符串时会发生什么?如果
len==0
,佩林会做什么?谢谢你的建议。我添加了几行来包含这些可能性,但它是ill不起作用。谢谢!但是它会超过时间限制。如果有任何问题,你可以用它来验证。
#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;
}
#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;
}