Statistics daffodils how many?

Narcissistic number: the so-called refers to a three-digit number daffodils, the digits of its cube is equal to the number itself. Example: 153 is a number daffodils. 153 = 1x1x1 + 5x5x5 + 3x3x3
Analysis:

0)定义一个统计变量
     int count = 0 ;
1)水仙花:就是三位数 ---->for循环 循环中的变量为x  100 ~999
    特点: 每个位的数据的立方和是当前数据本身
        153 

2)获取每个位上的数据
        int ge= x % 10 ;
        int shi = x /10 % 10 ;
        int bai = x /10 /10 % 10 ;

3)每个位上的数据满足条件
    x == ge*ge*ge+shi*shi*shi+bai*bai*bai

4)满足上面的条件 :count++
5)for循环的输出count值
//定义统计变量
int count = 0 ;

//for循环
for(int x = 100 ; x < 1000; x ++){
    //获取每个位上的数据;
    int ge= x % 10 ;
    int shi = x /10 % 10 ;
    int bai = x /10 /10 % 10 ;

    //满足条件
    if(x==(ge*ge*ge+shi*shi*shi+bai*bai*bai)){
        //输出水仙花数
        System.out.println(x) ;
        //统计变量++
        count++ ;
    }
}
System.out.println("所有的水仙花数共有:"+count+"个");

Guess you like

Origin blog.51cto.com/14651315/2463206