求出0~999之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本 身,如;153=1+5+3?,则153是一个“水仙花数“。)

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 1; i <= n ; i++) {
int count = 0;
int tmp = i ;
while (tmp != 0) {
count++;
tmp = tmp/10;
}
tmp = i;
int sum = 0;
while (tmp != 0) {
sum += Math.pow(tmp%10,count);
tmp = tmp/10;
}
if(sum == i) {
System.out.println(i);
}
}

原文地址:https://www.cnblogs.com/LinYanyan1024-6285/p/14242676.html