Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
2年前关闭。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
2年前关闭。
#include<stdio.h>
int palindrome(int n);
int main()
{
int x;
scanf("%d",&x);
palindrome(x);
return 0;
}
int palindrome(int n)
{
int r,re=0;
while(n !=0)
{
r=n%10; // remainder= the number modulus 10
re=r+re*10;
n=n/10;
}
if(n==re) // if the number is equal to the reverse number then it is
// palindrome otherwise not palindrome.
printf("Palindrome");
else
printf("Not Palindrome");
}
最佳答案
当您的代码到达while
函数中的palindrome
循环之外时,n
的值已变为0,然后将其与re
进行比较,该n
是n
的反向版本,答案为“否”。每次回文,除非n
的值是强文本本身。
你应该:
将re
的值复制到某个变量中,然后将其与re
进行比较,而不是将n
与return
进行比较。
在palindrome
函数的末尾放置一个虚拟。
评论回文,否则不回文。评论/部分。