gpt4 book ai didi

c - 回文程序中的逻辑错误

转载 作者:行者123 更新时间:2023-11-30 19:45:24 25 4
gpt4 key购买 nike

问题-
反转三位数的数字。并检查它们是否是回文。
逻辑错误:以下代码始终为所有输入显示“不是回文数”。

   d=0;
scanf("%d",&n);
a=n;

for(i=1;i<=3;i++) //loop will run 3 times corresponding to number of digits
{
c=a%10;
a=a/10;

for(f=100;f>=1;f=f/10) //for arranging digits in reverse order in number
{
d=d+f*c;
}

}
if(d==n) //checking whether number is palindrome or not
printf("palindrome");
else printf("not a palindrome");

最佳答案

d=0;
f=100;
scanf("%3d", &n);
a=n;

for(i=1;i<=3;i++){
c=a%10;
a=a/10;

d=d+f*c;
f=f/10;// f/=10;
}
if(d==n)
printf("palindrome");
else
printf("not a palindrome");

关于c - 回文程序中的逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26494249/

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