日期:2014-05-20 浏览次数:20777 次
import java.util.*;
import static java.lang.Math.*;
public class NarcissusTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("输入一个位数大于等于3的正整数,以显示出该数字范围内的所有水仙花数:");
String num = in.nextLine();
long ln = Integer.parseInt(num);
if (ln<100)
System.out.println("非法输入!");
ArrayList<Double> k = new ArrayList<Double>();
long sum = 0;
for (int i=100;i<=ln;i++) {
String str1 = ""+i;
int str1_n = str1.length();
boolean flag = true;
for (int j=0;j<str1_n;j++) {
char ch = str1.charAt(j);
String str2 = "" + ch;
k.add(Double.parseDouble(str2));
sum += pow(k.get(j),str1_n);
}
if (sum != i) {
flag = false;
break;
}
if (flag)
System.out.print(i+",");
}
}
}
// 计算数字i中每一位的立方和
for (int n : bits) {
sum += n * n * n;
}
// 计算数字i中每一位的立方和
for (int n : bits) {
sum += (int)Math.pow(n, bits.size());
}