- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
问题是找到第一个大于的回文数用户输入的数字。
事实上,我的代码为我尝试过的所有测试用例提供了正确的输出。但是我在 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/
我陷入了一项作业,很大程度上是因为我的教科书和类 Material 中极度缺乏示例,甚至是相关图表。 我按照自己的方式构建程序的原因是因为我需要使用 4 个方法:一个执行所有其他方法的主方法、一个检索
我尝试使用列表编写 Prolog 程序。但是,我必须使用 差异列表和输出应该是: 列表的第 i 个元素与列表的第 (n-i+1) 个元素相同,n 是列表的长度。例如,[a,X,c,b,Y]应该给 X
我一直在做Python学习任务,我遇到了这个任务,我必须读取一个包含几个单词的文件,并且一行是否是回文(向后写时相同:哈哈>哈哈)所以我尝试使用此代码,但它不会在终端上打印任何内容: with ope
我是 C# 的新手,我正在制作一个小应用程序来检查控制台的输入是否为回文。我自己走了很远,但我遇到了一个错误。 代码: class Program { static void Main(str
我想求出3位数字中最大的回文数。这是我的代码: for (int start = 100; start biggest) { biggest = temp;
我试着写了一个叫做 is_palidrome_v3(s) 的方法来比较单词的第一个和最后一个字母,然后是第二个和最后一个第二个字母,直到这对不一样,最后检查索引是否 >= len(s )//2 def
我正在编写一个函数来反转字符串并检查该字符串是否为回文。当我用像“abba”这样明显的回文来测试函数时,函数说它不是回文。此外,正向字符串和反向字符串的字符串长度也不同! #include #inc
我有一个纯 ASCII 字符串,它要么已经是一个回文串,要么可以通过删除一个字符变成回文串。我需要确定它是否已经是回文,如果不是,我需要找到需要删除的字符的索引。比如字符串是'aaba',那么去掉第一
美好的一天!我们的老师要求我们确定一个单词或一系列数字是否是回文或不使用堆栈。我已经做完了。但是我想多练习,所以现在我正在尝试通过删除空格和其他不相关的字符来确定一个句子是否是回文(注意:不再是我的作
回文是可以从任一方向以相同方式阅读的单词、短语、数字或其他单位序列。编写一个函数来确定给定的单词或数字是否为回文。 示例 >>> isPalindrome("") False >>> isPalind
with open('33.txt') as text: for line in text: line2 = line[:][::-1] if line ==
我正在做一些 python 在线教程,但我遇到了一个练习:A palindrome is a word which was spelled forwards as backwards.例如,单词 赛车
所以几个月前我在参加编程面试时,由于某种原因这个问题让我绊倒了。我可以想到几个解决方案,但其中大多数似乎效率极低。虽然多年来我一直以某种身份进行编程,但我目前正在大学攻读 CS 学位,所以我的引用点可
我正在尝试解决以下面试练习题: A k-palindrome is a string which transforms into a palindrome on removing at most k
无论我输入什么,我的代码都会打印出“true”。我的代码没有显示任何错误,我输入 56 作为输入,它仍然打印“true”。我真的很难弄清楚为什么。另外,我对 Java 和 Stack Overflow
我正在尝试弄清楚如何在给定的特定单词中找到回文。即 - banana 回文将是 anana。我有一个功能可以让我检查整个单词是否是一个,但我不知道如何检查单词的任何部分是否是回文。关于什么可以解决这个
/* 但如果我将第 13 行更改为“else return palindrome(s,++f, --l);”然后代码运行良好。这是什么原因?*/ #include #include #include
我想知道为什么结果不正确。如果我键入字符串来测试该字符串是否为回文,每当我尝试时,结果都是一样的。 例如,如果我输入“爸爸”进行测试,这表明它不是回文。但是,如果我输入“god”,它仍然告诉我这不是回
完全披露:这是一项作业,所以请不要发布实际的代码解决方案! 我有一个作业要求我从用户那里获取一个字符串并将其传递到堆栈和队列中,然后使用这两者来比较字符以确定该字符串是否为回文。我已经编写了程序,但是
如何在 C++ 中创建回文函数?我正在使用 2 种函数类型(bool 和 void)。到目前为止,这是我的代码(我真的很感激任何帮助,为什么我的代码不起作用?)谢谢! #include #inclu
我是一名优秀的程序员,十分优秀!