(例如:當n為3時,有1^3 + 5^3 + 3^3 = 153,153即是n為3時的一個自冪數)
n為1時,自冪數稱為獨身數。
n為2時,沒有自冪數。
n為3時,自冪數稱為水仙花數。
n為4時,自冪數稱為玫瑰花數。
n為5時,自冪數稱為五角星數。
n為6時,自冪數稱為六合數。
n為7時,自冪數稱為北鬥七星數。
n為8時,自冪數稱為八仙數。
n為9時,自冪數稱為九九重陽數。
n為10時,自冪數稱為十全十美數。
 複製代碼 代碼如下:
 /*
 
 * 自冪數
 
 * 自冪數是指一個 n 位元,它的每個位上的數位 n 次冪之和等於它本身。
 
 * (例如:當n為3時,有1^3 + 5^3 + 3^3 = 153,153即是n為3時的一個自冪數)
 
 */
 
import java.util.Scanner;
 
 public class 自冪數 {
  public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.println("請輸入您所要查的自冪數位元:");
  int n = input.nextInt();// 接收數字位元
  // 定義一數字number,從10^(n-1)開始自加,到10^n結束
  switch (n) {
  case 1:
   System.out.println("獨身數:");
   System.out.print("0" + "\t");
   break;
  case 2:
   System.out.println("兩位自冪數:");
   System.out.println("沒有自冪數!");
   break;
  case 3:
   System.out.println("水仙花數:");
   break;
  case 4:
   System.out.println("玫瑰花數:");
   break;
  case 5:
   System.out.println("五角星數:");
   break;
  case 6:
   System.out.println("六合數:");
   break;
  case 7:
   System.out.println("北鬥七星數:");
   break;
  case 8:
   System.out.println("八仙數:");
   break;
  case 9:
   System.out.println("九九重陽數:");
   break;
  case 10:
   System.out.println("十全十美數:");
   break;
  default:
   System.out.println("其它自冪數:");
   break;
  }
  for (int number = (int) Math.pow(10, n - 1); number < Math.pow(10, n); number++) {
   // 判斷條件:數字number的位元為n
   if (String.valueOf(number).length() == n) {
    double num = 0;
    for (int i = 0; i < n; i++) {
     int temp = (int) (number / Math.pow(10, i)) % 10;
     num += Math.pow(temp, n);
    }
    if (number == num) {
     System.out.print(number + "\t");
    }
   }
  }
  input.close();
 }
}