循环结构找出全部水仙花数,然后画出内存图。水仙花数是一个3位数,它等于其各位数字的立方和,比如 407 = 4*4*4+0*0*0+7*7*7。
1 #include<stdio.h>
2 int main()
3 {
4 int daffodil,hundreds,tens,units; //分别定义水仙花、百位数、十位数、个位数
5 for(daffodil=100;daffodil<1000;daffodil++) //因为水仙花为三位数,所以定义水仙花初值为100,循环至1000
6 {
7 hundreds=daffodil/100; //分别提出水仙花的百位数、十位数、个位数
8 tens=daffodil/10%10; //
9 units=daffodil%10; //
10 if(daffodil==(hundreds*hundreds*hundreds)+(tens*tens*tens)+(units*units*units)) //判断提出来的百、十、个位数是否满足水仙花=各位数的立方和
11 {
12 printf("水仙花 =%d\n",daffodil); //满足则打印
13 }
14 }
15 return 0;
16 }
版权声明:本文为weixin_72949140原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。