我使用 Code::Block 10.02 和当在 C.I 中打印 Amstrong Number 时,我等待这样的结果:

153
370
371
407

但它只是打印:
153
370
371

这是我在 C 中的代码:
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{

    int a,b,c;
    for  (a=1;a<=9;a++)
        for(b=0;b<=9;b++)
            for(c=0;c<=9;c++)
            {
                    if(pow(a,3)+pow(b,3)+pow(c,3)==100*a+10*b+c)
                           printf("\n%d%d%d",a,b,c);
            }

}

这是我的屏幕:http://daynhauhoc.com/uploads/default/3011/9598a2ad2183198f.png

但是,当我使用此代码时,效果很好:
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{

    int a,b,c;
    for  (a=1;a<=9;a++)
        for(b=0;b<=9;b++)
            for(c=0;c<=9;c++)
            {
                    int d=pow(a,3)+pow(b,3)+pow(c,3);
                    int e=100*a+10*b+c;
                    if(d==e)
                           printf("\n%d%d%d",a,b,c);
            }

}

有人告诉我一个解释吗?

最佳答案

哦,我知道为什么!!你用pow(a,3)+pow(b,3)+pow(c,3)==100*a+10*b+c 但是pow() 返回double 你用== 来判断float 和integer 是否相同。
不要在整数和浮点数之间使用 ==!=!

关于c - C语言打印阿姆斯壮数时缺少407,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30101701/

10-15 01:14
查看更多