Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 下一个回文代码_C - Fatal编程技术网

C 下一个回文代码

C 下一个回文代码,c,C,问题是找到第一个大于的回文数 用户输入的号码 事实上,我的代码为我尝试过的所有测试用例提供了正确的输出。但是我在spoj上得到了错误的答案。我还检查了没有空白或额外的行正在打印。我尝试了808213,1999和更多的输入 我该怎么办?下面是我的代码。此外,它没有超过时限 #include<stdio.h> void palindrome(int n) { int array[10],len,temp,i ; temp = n; len = 0 ; while(

问题是找到第一个大于的回文数 用户输入的号码

事实上,我的代码为我尝试过的所有测试用例提供了正确的输出。但是我在spoj上得到了错误的答案。我还检查了没有空白或额外的行正在打印。我尝试了808213,1999和更多的输入

我该怎么办?下面是我的代码。此外,它没有超过时限

#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;
}
#包括
无效回文(int n)
{
int数组[10],len,temp,i;
温度=n;
len=0;
while(温度!=0)
{
阵列[len]=温度%10;
len++;
温度=温度/10;
}
//当号码的形式为99999999时,依此类推

对于(i=0;i回文工作非常简单。它包括一个镜像阶段,然后检查镜像是否大于实际数量。如果不是,则添加中间值并重新镜像。下面是执行该操作的代码。您可能需要一些较小的重构来满足您的确切需要,但这应该会让您得到一个新的结果沿着完成的道路前进

#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;
}
#包括
#包括
int回文(int-n);
int镜像(intn);
内部主(空){
int-num;
num=回文(4549534);
printf(“%d\n”,num);
返回0;
}
int回文(int n){
int数组[10],len,temp,new_num,奇数,limit;
len=0;
温度=n;
while(温度!=0){
阵列[len]=温度%10;
len++;
温度=温度/10;
}
//这些值在镜像功能之外是必需的。
//好的代码样式将使这些类具有值。
奇数位=(长度%2);
限值=len/2+奇数;
new_num=镜像(n);
如果(新数量
您的代码在输入
100
时,结果是
111
,而不是
101