java打印输出所有的水仙花数..拜托了各位,我没分了

2024-11-18 12:40:32
推荐回答(2个)
回答(1):

public static void main(String[] args) {
int temp=0;
for (int i = 100; i < 999; i++) {
temp=i;
int x = temp % 10;
temp = temp / 10;
int y = temp % 10;
int z = temp / 10;
if (i == x * x * x + y * y * y + z * z * z) {
System.out.print(i+"\t");
}
}
}

回答(2):


class Narcissus{

public static void main(String[] args){

for(int x=100;x<1000;x++){

int ge = x%10;

int shi = x/10%10;

int bai = x/10/10%10;

if(ge*ge*ge+shi*shi*shi+bai*bai*bai == x)

System.out.println(x);

}

}

}