求100-1000所有的水仙花数,每行打印两个水仙花数,并打印水仙花的个数;
水仙花数就是一个三位数的个位十位百位各自的立方和等于这个三位数本身的数,比如371 = 3^3 + 7^3 + 1^3;371就是一个水仙花数;
public class ShuiXianHua{
public static void main(String[] args){
int count = 0;
for(int i = 100; i <= 999; i++){
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100 % 10;
//判定是否是水仙花数
if(ge*ge*ge + shi*shi*shi + bai*bai*bai == i){
System.out.print(i + " ");
count++;
//每行打印两个水仙花数
if(count % 2 == 0){
System.out.println();
}
}
}
//打印水仙花数的个数
System.out.print("水仙花数个数有" + count + "个");
}
}