一个整数的每个位上的数字的 n 次幂之和等于它本身。

解决方案 »

  1.   

    刚研究了下 这个应该可以
    package test;public class TestPower { public static void main(String[] args) {
    int mi = 3;
    int n = 100000;
     System.out.println("--------------" + mi + "次幂--------------");
     //打印所有小于n的证书,并且每个位上的数字的 mi次幂之和等于它本身的整数。
     for (int i = 0; i < n; i++) {
     if (power(i, mi)) {
     System.out.println(i);
     }
     }
    } // n表示这个数,mi表示mi次幂,如n=123,mi=2:n!=1^2+2^2+3^2
    public static boolean power(int n, int mi) {
    char[] arrInt = split(n);
    int k = 0;
    for (int i = 0; i < arrInt.length; i++) {
    k = k + (int) Math.pow(Character.digit(arrInt[i],10), mi);
    }
    //System.out.println("k=" + k);
    if (n == k) {
    return true;
    } else {
    return false;
    } } public static char[] split(int n) {
    String str = Integer.valueOf(n).toString();
    return str.toCharArray();
    }}