有點亂,純粹分享XD
package Homework;
import java.util.Arrays;
import java.util.Scanner;
public class Armstrong {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sca = new Scanner(System.in);
while (sca.hasNext()) {
String num = sca.nextLine();// 起始值
String num2 = sca.nextLine();// 末值
int u = num2.length();// 幾位數
int q = Integer.parseInt(num);
int w = Integer.parseInt(num2);
int d = w - q;// 變數範圍
String[] a = new String[d];
int n = 0;
for (int i = q + 1; i <= w; i++) {// 把變數範圍放入陣列
String g = Integer.toString(i);
a[n] = g;
n++;
}
for (int i = 0; i < d; i++) {
int h = a[i].length();// 陣列中該數的長度
int p = Integer.parseInt(a[i]);
switch (h) {
case 3:// 百
int a1 = p / (int) Math.pow(10, 0) % 10;
int a2 = p / (int) Math.pow(10, 1) % 10;
int a3 = p / (int) Math.pow(10, 2) % 10;
if ((int) Math.pow(a1, 3) + (int) Math.pow(a2, 3) + (int) Math.pow(a3, 3) == p) {
System.out.print(p + " ");
}
break;
case 4:// 千
int a4 = p / (int) Math.pow(10, 0) % 10;
int a5 = p / (int) Math.pow(10, 1) % 10;
int a6 = p / (int) Math.pow(10, 2) % 10;
int a7 = p / (int) Math.pow(10, 3) % 10;
if ((int) Math.pow(a4, 4) + (int) Math.pow(a5, 4) + (int) Math.pow(a6, 4)
+ (int) Math.pow(a7, 4) == p) {
System.out.print(p + " ");
}
break;
case 5:// 萬
int a8 = p / (int) Math.pow(10, 0) % 10;
int a9 = p / (int) Math.pow(10, 1) % 10;
int a10 = p / (int) Math.pow(10, 2) % 10;
int a11 = p / (int) Math.pow(10, 3) % 10;
int a12 = p / (int) Math.pow(10, 4) % 10;
if ((int) Math.pow(a8, 5) + (int) Math.pow(a9, 5) + (int) Math.pow(a10, 5) + (int) Math.pow(a11, 5)
+ (int) Math.pow(a12, 5) == p) {
System.out.print(p + " ");
}
break;
case 6:// 十萬
int a13 = p / (int) Math.pow(10, 0) % 10;
int a14 = p / (int) Math.pow(10, 1) % 10;
int a15 = p / (int) Math.pow(10, 2) % 10;
int a16 = p / (int) Math.pow(10, 3) % 10;
int a17 = p / (int) Math.pow(10, 4) % 10;
int a18 = p / (int) Math.pow(10, 5) % 10;
if ((int) Math.pow(a13, 6) + (int) Math.pow(a14, 6) + (int) Math.pow(a15, 6)
+ (int) Math.pow(a16, 6) + (int) Math.pow(a17, 6) + (int) Math.pow(a18, 6) == p) {
System.out.print(p + " ");
}
break;
case 7:// 百萬
int a19 = p / (int) Math.pow(10, 0) % 10;
int a20 = p / (int) Math.pow(10, 1) % 10;
int a21 = p / (int) Math.pow(10, 2) % 10;
int a22 = p / (int) Math.pow(10, 3) % 10;
int a23 = p / (int) Math.pow(10, 4) % 10;
int a24 = p / (int) Math.pow(10, 5) % 10;
int a25 = p / (int) Math.pow(10, 6) % 10;
if ((int) Math.pow(a19, 7) + (int) Math.pow(a20, 7) + (int) Math.pow(a21, 7)
+ (int) Math.pow(a22, 7) + (int) Math.pow(a23, 7) + (int) Math.pow(a24, 7)
+ (int) Math.pow(a25, 7) == p) {
System.out.print(p + " ");
}
break;
}
}
}
}
}