CCHERMIT
2015-04-25 15:37
#include <stdio.h>
int main()
{
int num, sd, td, hd;
for(num=100;num<1000;num++)
{
hd =num/100;
td =(num%100)/10;
sd =num%10;
if(num==hd*hd*hd+td*td*td+sd*sd*sd)
{
printf("水仙花数字:%d\n", num);
}
}
return 0;
}如果第10行为 if(num=hd*hd*hd+td*td*td+sd*sd*sd) 输出的数字中包含一位数的数字;
为什么会这样?
这是因为num=hd*hd*hd+td*td*td+sd*sd*sd为赋值语句即将hd*hd*hd+td*td*td+sd*sd*sd所得的结果赋值给num,而且赋值语句一般都是能成功的所以返回值为true,所以会进入if语句输出num为一位数。例如第一次循环hd=1,td=0,sd=0,num=1,输出结果为"水仙花数字:1"
C语言入门
928084 学习 · 21543 问题
相似问题