水仙花数:一个三位数,其各位数字的立方和是其本身
例如:
153--
个位3: 153 % 10 =3
十位5: 153 /10 %10 =5
百位1: 153 /10 /10 %10 =1
public static void main(String[] args) { System.out.println("100-1000中的水仙花数有:"); for(int i=100;i<1000;i++){ int ge = i%10; int shi = i/10%10; int bai = i/10/10%10; //水仙花数判断要求 if(i == (Math.pow(ge, 3)+Math.pow(shi, 3)+Math.pow(bai, 3))){ //Math.pow(a,b) a:数字 b:几次方 System.out.println(i); } } }
实现代码如上
i%10 求i除以10的余数
i/10 求i里边有多少个10(结果为整数)