20151015查找水仙花数

 /*设计一个程序,找出100~999内所有的"水仙花数".
"水仙花数"的定义是:三位数的各位数字立方和等于这个三位数本身,
例如153=1*1*1+5*5*5+3*3*3.*/


#include <stdio.h>
#include <Windows.h>
int main()
{
    //定义三位数num,个位数sd,十位数td,百位数hd
    int num, sd, td, hd;
    //循环所有三位数
    for(   num=100  ;  num<1000   ;   num++  )
    {
        //获取三位数字num百位上的数字
        hd =        num/100         ;
        //获取三位数字num十位上的数字
        td =        (num%100)/10;
        //获取三位数字num个位上的数字
        sd =        num%10         ;
        //水仙花数的条件判断
        if( num==hd*hd*hd+td*td*td+sd*sd*sd                 ) 
        {
            printf("水仙花数字:%d\n", num);    
        }
    }
    system("pause");
    return 0;    
}



posted on 2017-10-05 18:17  sunshineman1986  阅读(137)  评论(0)    收藏  举报

导航