1 / 5
Dec 2018

Guys my code is correct according to me and it shows correct output against boundry test cases but it shows error on submission says that wrong answer please review my code.
#include
using namespace std;
int check_palindrome(int a)
{ int t=a,r=0,flag;
while(t!=0)
{ r=r*10;
r=r+(t%10);
t=t/10;
}
if(r==a)
flag=1;
else
flag=0;
return flag;
}
int main()
{ int b,j=0;
cin>>b;
for(int i=1;i<=b;i++)
{ int a;
cin>>a;

     int j=0;
   while(j==0)
   {  ++a;
   if(a==1000000)
    break;
     int flag=check_palindrome(a);
       if(flag==1)
    { cout<<a;
      cout<<"\n";
       j=1;
    }
   }
 }

return 0;
}

  • created

    Dec '18
  • last reply

    Apr '20
  • 4

    replies

  • 942

    views

  • 4

    users

Try this test case:

1
123456789012345678901234567890123456789012345678901234567890

1 year later

what is wrong with my code ?? spoj gives wrong answer!!
#include
#include
#include
using namespace std;
class num{string s;vector v;
public:
int l;
void store();
void odd();
void even();
};
void num::store(){ cin>>s; l=s.length();
for(int i=0;i<l;i++){v.push_back(s[i]-‘0’);}
}
void num::even(){int m=l/2; int x=0,y=0;
for(int i=0;i<m;i++){
if(v[m+i]<v[m-1-i]){v[m+i]=v[m-1-i]; // rest of elements to be equalled
break;
}
if(v[m+i]>v[m-1-i]){for(int j=0;;j++){
if(v[m-1-j]==9){v[m-1-j]=0;}
else{v[m-1-j]++;break;
}
}break;
}
if(v[m+i]==v[m-1-i]){if(v[m+i]==9){x–;}
else y–;}
}
if(x==-m){cout<<“1”;v.assign(l,0);v[l-1]=1;}
else if(x+y==-m){
for(int j=0;j<m;j++){
if(v[m-1-j]==9){v[m-1-j]=v[m+j]=0;continue;}
else{v[m+j]=++v[m-1-j];break;}
}
}
else for(int i=0;i<m;i++){v[m+i]=v[m-1-i];} //making the palin
for(int i=0;i<l;i++){cout<<v[i];} //print
}
void num::odd(){int m=(l-1)/2;int x=0,y=0; //m=0
for(int i=1;i<=m;i++){
if(v[m+i]<v[m-i]){break;}
if(v[m+i]>v[m-i]){ for(int j=0;j<i;j++){
if(v[m-j]==9){v[m-j]=0;continue;}
else{v[m-j]++;break;}
}break;
}
if(v[m+i]==v[m-i]){if(v[m-i]==9){x–;}
else y–;}
}
if(x==-m){ // all 99
if(v[m]==9){cout<<“1”;v.assign(l,0);v[l-1]=1;}
else{v[m]++;}
}
else if(x+y==-m){ // a palindrome but not all 9
for(int j=0;j<=m;j++){
if(v[m-j]==9){v[m-j]=v[m+j]=0;continue;}
else{v[m+j]=++v[m-j];break;}
}
}
else for(int i=1;i<=m;i++){v[m+i]=v[m-i];} //making the palin
for(int i=0;i<l;i++){cout<<v[i];} //print
}
int main(){ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;cin>>t;
num n[t];
for(int i=0;i<t;i++){
n[i].store();
}
for(int i=0;i<t;i++){
if(n[i].l==1)cout<<“11”;
else if(n[i].l%2==0)n[i].even();
else n[i].odd();
cout<<"\n";
}
return 0;
}

Are the SPACE and the TAB buttons in your keyboard broken!?

Try these;

3
0
1
19995

(If you want us to read your code – instead of simply testing it – then you need to make it readable.)