JAVA 打印水仙花数

时间:2023-02-23 23:22:14

求100-999中的水仙花数,(若三位数abc,abc=a^3+b^3+c^3, 则称abc为水仙花数。如153,1^3+5^3+3^3=1+125+27=153,则153是水仙花数)

package Forxh;



public class Test2 {
public static void main(String[] args) {

//2.打印水仙花数(玫瑰花数)
for (int i = 100; i <= 999; i++) {
// 取个位
int g = i / 1 % 10;
// 取十位
int s = i / 10 % 10;
// 取百位
int b = i / 100 % 10;
if(i==Math.pow(g, 3)+Math.pow(s, 3)+Math.pow(b, 3)){
System.out.println(i);
}
}


}

}