gpt4 book ai didi

c - 下一个回文码

转载 作者:太空宇宙 更新时间:2023-11-04 08:25:23 25 4
gpt4 key购买 nike

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com