问答详情
源自:4-9 循环结构之for循环(二)

哪位大神解释一下为什么num的循环是100到1000

为什么num的循环是从100到1000的循环呢?哪位大神解释一下呗

提问者:都是孩子 2015-01-30 17:51

个回答

  • diffcolt
    2015-03-04 19:33:25

    你没有一个变量赋值啊,

  • 千万里万望珍重
    2015-01-30 18:05:36

    水仙花是指一个三位数,其各位数字立方和等于该数,故范围是100<=num<=999;for循环中可以用num<=999,也可以用num<1000,这两个都是一样的




    #include <stdio.h>

    int main()

    {

        //定义三位数num,个位数sd,十位数td,百位数hd

        int num, sd, td, hd;

        //循环所有三位数

        for(   num=100  ;  num<=999   ;    num++  )

        {

            //获取三位数字num百位上的数字

            hd =        num/100         ;

            //获取三位数字num十位上的数字

            td =        num%10        ;

            //获取三位数字num个位上的数字

            sd =        (num/10)%10         ;

            //水仙花数的条件是什么?

            if(     hd*hd*hd+td*td*td+sd*sd*sd==num                 ) 

            {

                printf("水仙花数字:%d\n", num);    

            }

        }

        return 0;    

    }