java水仙花數

hl_ks發表於2024-10-17

public class NarcissisticNumber {
public static void main(String[] args) {
for (int i = 100; i < 1000; i++) {
int hundredsDigit = i / 100;
int tensDigit = (i / 10) % 10;
int onesDigit = i % 10;
int sumOfCubes = (int) (Math.pow(hundredsDigit, 3) + Math.pow(tensDigit, 3) + Math.pow(onesDigit, 3));
if (sumOfCubes == i) {
System.out.println(i);
}
}
}
}

相關文章