编写一个程序,读取一个整数。输出另一个整数,偶数位保留,奇数位减1,
这是我的代码:
#include <stdio.h>
#include <math.h>
int main() {
int num, i = 0, new_num = 0, u;
printf("Enter a number: ");
scanf("%d", &num);
while (num > 0) {
u = num % 10;
if (u % 2 == 0)
u = u;
if (u % 2 == 1)
u = u - 1;
new_num = new_num + u * pow(10, i);
num = num / 10;
i++;
}
printf("The new number is: %d", new_num);
return 0;
}
现在,当我在 gcc(VS Code) 中执行此操作时,对于 2 位数字,一切正常。但对于超过三位数的数字,我会收到错误。如输入=23145
输出=22043
。但我期待输出=22044
。
另外,如果我在 DevC/C++ 中运行相同的代码,也不会出现错误。
谁能帮我解决这个问题吗?
Here is my code:
#include <stdio.h>
#include <math.h>
int main() {
int num, i = 0, new_num = 0, u;
printf("Enter a number: ");
scanf("%d", &num);
while (num > 0) {
u = num % 10;
if (u % 2 == 0)
u = u;
if (u % 2 == 1)
u = u - 1;
new_num = new_num + u * pow(10, i);
num = num / 10;
i++;
}
printf("The new number is: %d", new_num);
return 0;
}
Now, when I am doing this in gcc(VS Code), for 2-digit number everything is ok. But for digits more than three I am getting a error. Like Input=23145
Output=22043
. But I was expecting output=22044
.
Also, if I run the same code in DevC/C++, there is no error.
Can anyone help me out in this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的程序在我的系统上产生预期的输出:
22044
for23145
,但这可能取决于pow
函数的实现。获得不同输出的原因可能是 C 库中的
pow()
函数精度问题的副作用:如果实现了pow(x, y)
如exp(y * log(x))
,x
和y
的整数值的结果可能非常接近,但低于实际值整数值,导致转换为int
以生成先前的值整数。一些 C 库作者对整数参数进行了特殊处理以避免此问题,但强烈建议避免使用浮点函数进行整数运算,以防止出现此类棘手问题。我建议您对代码进行更多更改:
测试
scanf()
的返回值。删除
if (u % 2 == 0) u = u;
部分,它没有任何效果。无论如何,应该有一个
else
子句,以便在测试奇数位时不使用前一种情况的结果。不要使用浮点函数
pow()
:只需保留一个乘数变量并在循环中更新它。该程序不处理负数。
这是修改后的版本:
请注意,
digit -= digital % 2;
将减少正奇数位,并实际增加负奇数位,这实际上总是减少奇数位的绝对值。这样,正值和负值都可以正确处理。Your program produces the expected output on my system:
22044
for23145
, but this might depend on the implementation of thepow
function.The reason you get a different output is probably a side effect of a precision issue with the
pow()
function in your C library: ifpow(x, y)
is implemented asexp(y * log(x))
, the result for integral values ofx
andy
could be very close but inferior to the actual integral value, causing the conversion toint
to produce the previous integer. Some C library authors make a special case of integral arguments to avoid this problem, but it is highly recommended to avoid floating point functions for integer arithmetics to prevent such tricky issues.I would advise some more changes in your code:
test the return value of
scanf()
.remove the
if (u % 2 == 0) u = u;
part, it has no effect.in any case, there should be an
else
clause to not use the result of the previous case when testing for odd digits.do not use the floating point function
pow()
: just keep a multiplier variable and update it in the loop.the program does not handle negative numbers.
Here is a modified version:
Note that
digit -= digit % 2;
will decrement positive odd digits and actually increment negative odd digits, which effectively always decrements the absolute value of odd digits. This way both positive values and negative values are handled correctly.在我的 Visual Studio Code 实例中,对于输入 23145,我确实发现 22044 作为输出。
我猜想分歧是由
pow(10,i)
的演员阵容带来的。 C 中的 Pow 函数返回一个 double,这不是您真正想要的。我强烈建议不要使用pow
函数进行整数运算。解决方案可能是:
In my instance of Visual Studio Code, for an input 23145, I indeed find 22044 as an output.
I guess the divergence comes with the cast of
pow(10,i)
. Pow function in C returns a double which is not what you really want here. I strongly advice to not use thepow
function for integer arithmetic.A solution could be :
问题似乎出在使用返回双精度值的函数
pow
。如果您正在处理整数,那么最好避免使用返回双精度数的函数,因为可能会出现截断,然后双精度数会转换为整数。
另请注意,用户可以输入负数。你的程序允许这样做。在这种情况下,您的程序也会产生不正确的结果。
我会按照以下方式编写程序
程序输出是
如果即使负数添加 -1 那么你应该用这个 if 语句替换这个
在
这种情况下,程序输出可能看起来像
在这种情况下,这是新值负值
-123456789
将为-224466880
。It seems that the problem is using the function
pow
that returns a double value.If you are dealing with integers then it is better to avoid using functions that return doubles due to a possible truncation then a double is converted to an integer.
Also pay attention to that the user can enter a negative number. Your program allows to do that. In this case your program also will produce an incorrect result.
I would write the program the following way
The program output is
If even for negative digits to add -1 then you should substitute this if statement
for this one
In this case the program output might look like
That is in this case the new value for the negative value
-123456789
will be-224466880
.