package java基础知识.小结与练习;
public class Test9 {
/**
* @param 打印出所有的"水仙花数"。
所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
思路:
1:采用循环取得所有的三位数。(三位数指的是100-999之间的数)。
2:把每个三位数的个位,十位,百位进行分解。
*/
public static void main(String[] args) {
for (int i=100; i<=999;i++){ //循环遍历三位数
int a = i/100; //取三位数百位上的数字
int b = (i-a*100)/10; //取三位数十位上的数字 也可以写成int b = i/10%10;
int c = i%10; //取三位数上个位上的数字
if (i==(a*a*a+b*b*b+c*c*c)){ //if判断语句 判断 该三位数是否等于该三位数上各数字的立方和
int count = 0; //定义变量存储水仙花数出现的次数
count++; //对水仙花数出现的次数进行递增
//打印出水仙花数
System.out.println("第"+count+"个水仙花数是 :"+i);
}
}
/*for (int i = 100; i < 1000; i++) {
int x=i%10;
int y=i/10%10;
int z=(int)i/100;
if (i == x * x * x + y * y * y + z * z * z) {
System.out.print(i+"\t");
}*/
}
}