日期:2014-05-20  浏览次数:20758 次

水仙花数。 我要把三改成八 怎么改程序
[code=Java][/code]int a, b, c;
for (int i = 100; i < 1000; i++) {
a = i / 100;
b = i / 10 % 10;
c = i % 10;
int k = a * 100 + b * 10 + c;
if(a*a*a+b*b*b+c*c*c==k){
System.out.println(k);
}
}

------解决方案--------------------

Java code
a*a*a*a*a*a*a*a+b*b*b*b*b*b*b*b+c*c*c*c*c*c*c*c == k

------解决方案--------------------
Java code

long s, t, n = 8;
        for (long i = (long) Math.pow(10, n - 1); i < Math.pow(10, n); i++) {
            s = 0;
            for (int j = 1; j <= n; j++) {
                if (j == 1)
                    t = i % 10;
                else
                    t = (long) (i % Math.pow(10, j) / Math.pow(10, j - 1));
                s += Math.pow(t, n);
            }
            if (s == i) {
                System.out.println(s);
            }
        }
        System.out.println("over");

------解决方案--------------------
Java code

        long s, t, n = 4;
        for (long i = (long) Math.pow(10, n - 1); i < Math.pow(10, n); i++) {
            s = 0;
            for (int j = 1; j <= n; j++) {
                t = (long) (i % Math.pow(10, j) / Math.pow(10, j - 1));
                s += Math.pow(t, n);
            }
            if (s == i) {
                System.out.println(s);
            }
        }
        System.out.println("over");

------解决方案--------------------
Java code

       long s, t, n = 8;
        for (long i = (long) Math.pow(10, n - 1); i < Math.pow(10, n); i++) {
            s = 0;
            for (int j = 1; j <= n; j++) {
                t = (long) (i % Math.pow(10, j) / Math.pow(10, j - 1));
                s += Math.pow(t, n);
            }
            if (s == i) {
                System.out.println(s);
            }
        }
        System.out.println("over");