Java基础编程题——水仙花数

 1 package com.yangzl.basic;
 2 /**
 3  * 题目:打印出所有的"水仙花数".
 4  * 所谓"水仙花数"是指一个三位数,
 5  * 其各位数字立方和等于该数本身。
 6  * 例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。  
 7  * 
 8  * @author Administrator
 9  *
10  */
11 /*程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。*/
12 public class ShuiXianHua {
13     public static void main(String[] args) {
14         for(int i=101;i<1000;i++){
15             if(isSXH(i)==true){
16                 System.out.println(i+"为水仙花数");
17             }
18         }
19     }
20     /**
21      * 判断是否为水仙花的函数
22      * @return
23      */
24     public static boolean isSXH(int n){
25         boolean bl = false;
26         //注意:n是100~1000之间的三位数
27         int x = n/100;//百位
28         int y = (n%100)/10;//十位
29         int z = n%10;//个位
30         if(n == (x*x*x + y*y*y + z*z*z)){
31             bl = true;
32         }
33         return bl;
34     }
35 }

结果:

153为水仙花数
370为水仙花数
371为水仙花数
407为水仙花数
逃避不一定躲得过,面对不一定最难过
原文地址:https://www.cnblogs.com/yangzhenlong/p/3528453.html