我正在创建一个程序,使用以下方法显示数字的倒数:
示例:
123 ==> 3*10^2 + 2*10^1 + 1*10^1 = 321
但输出始终为 0。有什么帮助吗?
#include <stdio.h>
int power(int a)
{
int i;
int x = 1;
for (i = 1; i = a; i++)
{
x = x * 10;
}
return x;
}
int inv(int b)
{
int z = b, j = 0, s = 0, y;
for (z = b; z = 0; z = z / 10)
{
for (y = z; y = 0; y = y / 10)
{
j++;
}
s = s + (z % 10)*power(j - 1);
}
return s;
}
int main()
{
printf("please enter a number"); int n;
scanf("%d", &n);
printf("%d", inv(n));
return 0;
}
最佳答案
你认为这两个for循环什么时候会继续下去?
你认为他们什么时候会停止?
(提示:你错了。)
在调试器中观察 z 和 y 的值。
for (z = b; z = 0; z = z / 10)
for (y = z; y = 0; y = y / 10)
关于c++ - 数的倒数,C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36435272/