There is a problem on Pallindrome given in the site www.spoj.com. The problem statement is - For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros. Input
The first line contains integer t, the number of test cases. Integers K are given in the next t lines.
I have written a CPP code for this
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <sstream>
#include <bits/stdc++.h>
#include<limits>
using namespace std;
int main(){
int b;
cin>>b;
int num[b];
for(int i=0;i<b;i++){
cin>>num[i];
if(num[i]>1000000){
//cout<<"Enter value less than 1000000"<<endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
//cout<<"You have entered wrong input"<<endl;
cin>>num[i];
}
}
string a[b];
for(int i=0;i<b;i++){
long numb = num[i] + 1;
if(!(numb>1000000 || num[i] <10 )){
for(int k=numb; k<1000000; k++){
stringstream ss;
ss << k;
string str=ss.str();
string ostr = str;
reverse(str.begin(), str.end());
if(ostr == str){
a[i] = ostr;
//cout<<ostr<<endl;
break;
}
}
cout<<a[i]<<endl;
}
}
}
This is still not an acceptable answer. Where am I missing something. I have tested with lot of test cases and its working.
As per suggestions I changed it to this but still it was not accepted.
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <sstream>
//#include <bits/stdc++.h>
#include<limits>
using namespace std;
int main(){
int b;
cin>>b;
int num[b];
for(int i=0;i<b;i++){
cin>>num[i];
if(num[i]>1000000){
//cout<<"Enter value less than 1000000"<<endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
//cout<<"You have entered wrong input"<<endl;
cin>>num[i];
}
}
string a[b];
for(int i=0;i<b;i++){
long numb = num[i] + 1;
if(!(numb>1000000 || num[i] <10 )){
for(int k=numb; k<1000000; k++){
stringstream ss;
ss << k;
string str=ss.str();
string ostr = str;
int len = str.length();
int n=len-1;
for(int m=0;m<(len/2);m++){
//Using the swap method to switch values at each index
swap(str[m],str[n]);
n = n-1;
}
//reverse(str.begin(), str.end());
if(ostr == str){
a[i] = ostr;
//cout<<ostr<<endl;
break;
}
}
cout<<a[i]<<endl;
}
}
}