问题是要找到大于的第一个回文数 用户输入的数字。
实际上,我的代码为我尝试过的所有测试用例提供了正确的输出。但是我在spoj上得到了错误的答案。我还检查过没有空格或额外的行被打印。我已经尝试了808,2133,1,999等等作为输入。
我该怎么办?以下是我的代码。此外,它没有超过时限。
#include<stdio.h>
void palindrome(int n)
{
int array[10],len,temp,i ;
temp = n;
len = 0 ;
while(temp!=0)
{
array[len] = temp%10;
len++;
temp = temp/10;
}
//when the number is of the form 99,999,9999 and so on
for(i=0;i<len;i++)
{
if(array[i]!=9)
break;
}
if(i==len)
{
printf("%d",n+2);
return ;
}
if((len%2)==1)
{
//when the length is odd 0,1,2,3,4 and it does not consist of all 9s.
for(i=0;i<(len/2);i++)
{
array[i] = array[len-1-i];
}
//at this stage we again check if number is already of the form 9,99 999 or so on
for(i=0;i<len;i++)
{
if(array[i]!=9)
break;
}
if(i==len)
{
for(i=0;i<len;i++)
printf("%d",array[len-1-i]);
return ;
}
// if the number is not of the form 9 ,99 ,999 then
i=0;
while((array[(len/2)-i]==9)&&(i<=(len/2)))
{
array[len/2-i] = 0;
array[len-1-len/2+i] = 0 ;
i++;
}
array[len/2-i] = array[len/2-i] +1 ;
array[len-1-len/2+i] = array[len/2-i] ;
for(i=0;i<len;i++)
printf("%d",array[len-1-i]);
return ;
}
//if the len is even eg 6 , 0,1,2,3,4,5 6/2 = 3
for(i=0;i<len/2-1;i++)
{
array[i] = array[len-1-i];
}
if(array[len/2]!=9)
{
array[len/2-1] = array[len/2]+1 ;
array[len/2] = array[len/2-1] ;
for(i=0;i<len;i++)
printf("%d",array[len-1-i]);
return ;
}
//at this stage we again check if number is already of the form 99999 or 999 or so on
for(i=0;i<len;i++)
{
if(array[i]!=9)
break;
}
if(i==len)
{
for(i=0;i<len;i++)
printf("%d",array[len-1-i]);
return ;
}
i=0;
while(array[len/2-i-1]==9)
{
array[len/2-i-1] = 0;
array[len+i-len/2] = 0;
i++;
}
array[len/2-i-1] = array[len/2-i-1] +1;
array[len+i-len/2] = array[len/2-i-1];
for(i=0;i<len;i++)
printf("%d",array[len-1-i]);
return ;
}
int main()
{
int n,t,i;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&n);
palindrome(n);
printf("\n");
}
return 0;
}
答案 0 :(得分:2)
#include <stdio.h>
#include <math.h>
int palindrome(int n);
int mirror(int n);
int main(void) {
int num;
num = palindrome(4549534);
printf("%d\n", num);
return 0;
}
int palindrome(int n) {
int array[10],len,temp,new_num,odd_digits,limit;
len = 0;
temp = n;
while (temp!=0) {
array[len] = temp%10;
len++;
temp = temp/10;
}
// These values are needed outside of the mirror function.
// Good code style would make these class values.
odd_digits = (len % 2);
limit = len / 2 + odd_digits;
new_num = mirror(n);
if (new_num < n) {
// Palindromes increase from the middle.
new_num += (int) pow(10, limit - 1);
// Re-mirror the number.
new_num = mirror(new_num);
}
return new_num;
}
int mirror (int n) {
int array[10],len,temp,i,new_num,odd_digits,limit,top,bottom ;
temp = n;
new_num = 0;
len = 0 ;
temp = n;
while (temp!=0) {
array[len] = temp%10;
len++;
temp = temp/10;
}
odd_digits = (len % 2);
limit = len / 2 + odd_digits;
for (i = 0; i < limit; i++) {
top = array[(len - 1) - i] * (int) pow(10,((len - 1) - i));
bottom = array[(len - 1) - i] * (int) pow(10, i);
// Check to see if this is the middle term, in which case we only need to
// add one value.
if ((len - 1 - i) == i) {
bottom = 0;
}
new_num += top + bottom;
}
return new_num;
}