Next Palindrome SPOJ

我已经为下一个回文问题编写了一个powershell解决方案,并希望超出时间限制。 但是,当我测试一些测试用例时,它工作正常但是当我在spoj中提交代码时,我得到了错误的答案。 这是我的代码。 请有人帮我弄清楚这个程序代码有什么问题。

int main() { int t,k,cmp,tmp; scanf("%d",&t); //No of test cases while(t--) { scanf("%d",&k); //Enter the input while(1) { ++k; //Increment Every no if there is no palindrome tmp=k; cmp=0; while(tmp%10 != 0) // Reverse a number { cmp=(cmp*10)+(tmp%10); tmp=tmp/10; } if(k == cmp) // If Reverse and K are same its a palindrome . { printf("%d\n",k); break; } } } return 0; } 

以下是问题的链接: http : //www.spoj.com/problems/PALIN/您可以在以下url提交我的解决方案: http : //www.spoj.com/submit/PALIN/

 while (tmp%10 != 0) // Reverse a number { cmp = (cmp * 10) + (tmp % 10); tmp = tmp / 10; } 

如果您的号码中有0,如5403123,则tmp%10为0,并且它会停止反转该号码

问题是K不超过1000000位。 这意味着k可以拥有最多1000000个数字,你无法接受int 。 您必须将输入作为字符并存储在数组中。

char arr[1000001];

int i=0, c;

while(c = getchar() && c!='\n') { arr[i] = c; i++; }

并进一步实现您的算法。