在SPOJ上提交此代码时出现运行时错误(SIGSEGV)错误。我清除了向量并初始化了所有变量,但仍然得到了错误。这段代码有什么问题?
#include<iostream>
#include<vector>
using namespace std;
vector<int> even_greater_increment(vector<int> num,int mid1,int mid2){
int bigindex=0,smallindex=0;
if(num[mid1]==num[mid2]){
return num;
}
if(num[mid1]>num[mid2]){
bigindex=mid1;
smallindex=mid2;
}
else{
bigindex=mid2;
smallindex=mid1;
}
while(num[smallindex]!=num[bigindex]){
num[smallindex]++;
}
return num;
}
bool check9(vector<int> num,int vlen){
for(int i=0;i<vlen;i++){
if(num[i]==9){
continue;
}
else{
return false;
}
}
return true;
}
int nine(vector<int> num,int vlen){
vlen=vlen+1;
num[0]=1;
cout<<num[0];
for(int i=1;i<vlen-1;i++){
num[i]=0;
cout<<num[i];
}
num[vlen-1]=1;
cout<<num[vlen-1];
return 0;
}
vector<int> increment9(vector<int> num,int mid1,int mid2,int vlen){
int l=mid1,k=mid2;
do{
l--;k++;
}while((l>=0&&k<=(vlen-1))&&(num[l]==9&&num[k]==9));
num[l]++;
num[k]++;
for(int i=l+1;i<k;i++){
num[i]=0;
}
return num;
}
vector<int> incrementmiddle(vector<int> num,int mid1,int mid2,int vlen){
if(num[mid1]==9&&num[mid2]==9){
num=increment9(num,mid1,mid2,vlen);
return num;
}
if(mid1==mid2){//odd implementation
num[mid1]=num[mid1]+1;
}
else{//even implementation
num=even_greater_increment(num,mid1,mid2);
}
return num;
}
int println(vector<int> r,int vlen){
for(int p=0;p<vlen;p++){
cout<<r[p];
}
}
bool comp(const vector<int>& v1, const vector<int>& v2) {
if (v1.size() != v2.size())
return v1.size() < v2.size();
for (int i = 0; i < v1.size(); i++)
if (v1[i] != v2[i])
return v1[i] < v2[i];
return false;
}
int palindrome(){
int length=0;
char ch[7];
cin>>ch;
for(int i=0;ch[i]!='\0';i++){
length++;
}
vector<int> num;
vector<int> temp;
for(int j=0;j<length;j++){
int a= ch[j] - '0';
num.push_back(a);
}
int vlen=num.size();
int mid1=0,mid2=0;
if(vlen==1){
cout<<num[0]<<endl;
return 0;
}
if(vlen==2){
mid1=0;mid2=1;
int bigindex,smallindex;
if(num[mid1]>num[mid2]){
bigindex=mid1;
smallindex=mid2;
}
else{
bigindex=mid2;
smallindex=mid1;
}
num[smallindex]=num[bigindex];
println(num,2);
return 0;
}
if(vlen%2==0){
mid2=vlen/2;
mid1=mid2-1;
}
else{
mid1=mid2=vlen/2;
}
temp=num;
if(mid1==mid2){
int y=mid1;
//for odd implementation
for(int j=0;j<mid1;j++){
++y;
num[y]=num[j];
}
//num contains palindrome and temp contains original
if(comp(temp,num)){
println(num,vlen);
return 0;
}//printing the number if the palindrome is greater than the input number
else{
if(check9(num,vlen)){
nine(num,vlen);
return 0;
}
num=incrementmiddle(num,mid1,mid2,vlen);
println(num,vlen);
return 0;
}
}
else{//even implementation
int h=vlen-1;
for(int b=0;b<mid1;b++){
num[h]=num[b];
h--;
}
if(comp(temp,num)){
num=even_greater_increment(num,mid1,mid2);
println(num,vlen);
return 0;
}//printing the number if the palindrome is greater than the input number
else{
if(check9(num,vlen)){
nine(num,vlen);
return 0;
}
num=incrementmiddle(num,mid1,mid2,vlen);
println(num,vlen);
return 0;
}
}
num.clear();
temp.clear();
}
int main(){
int test=0;
cin>>test;
testcase:
for(int f=0;f<test;f++){
palindrome();
cout<<endl;
}
}
它实际上需要一个数字并打印下一个小回文。 逻辑和一切都是正确的。它在编译器和ideone中工作正常。 请帮忙。我真的很想接受这个问题,因为我花了5个多小时编写逻辑。