c - 下一个回文码

标签 c

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

事实上,我的代码为我尝试过的所有测试用例提供了正确的输出。但是我在 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;
}

最佳答案

回文的工作很简单。它包含一个镜像阶段,然后检查镜像是否大于实际数量。如果不是,则我们添加中间值并重新镜像。这是执行此操作的代码。您可能需要一些小的重构来满足您的确切需求,但这应该会让您顺利完成。

#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;
}

关于c - 下一个回文码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30740628/

相关文章:

c - (10%2) 是什么意思?

java - 从网页获取 Java 或 C 程序的输入?

c - 为什么 max 、min 和 canocalise 函数在 c 中不起作用?

c - 梯形法则问题

c - 如何在C中解决这个问题?

c - 为什么即使我们将正分数放入 double 类型,符号位仍为 1 的原因

C解密程序

c - SIGFPE C 中的错误?

c - 如何使 vector 在子程序中可用? (C)

c - 信号处理程序中更新的变量永远不会更新