Java实现0-100000内水仙花数的完整代码
对不起,我的回答有误。以下是完整的0到100000内的水仙花数:
153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084
以下是用Java语言实现的代码:
public class NarcissisticNumbers {
public static void main(String[] args) {
for (int i = 0; i <= 100000; i++) {
if (isNarcissistic(i)) {
System.out.println(i);
}
}
}
private static boolean isNarcissistic(int num) {
int sum = 0;
int temp = num;
int numDigits = String.valueOf(num).length();
while (temp > 0) {
int digit = temp % 10;
sum += Math.pow(digit, numDigits);
temp /= 10;
}
return sum == num;
}
}
原文地址: https://www.cveoy.top/t/topic/m4wP 著作权归作者所有。请勿转载和采集!