PS:這題滿分100,沒有作對,你們幫忙看看問題在哪java
/*
* 題目:水仙花數升級版
* 描述: 水仙花數是指一個 n 位數 ( n≥3 ),它的每一個位上的數字的 n 次冪之和等於它自己。(例如:1^3 + 5^3+ 3^3 = 153)
給你A和B,求[A,B]區間內有多少個水仙花數
題目類別: 循環,查找,枚舉,位運算
難度: 中級
分數: 100
運行時間限制: 無限制
內存限制: 無限制
階段: 應聘考試
輸入: 兩個正整數,用空格隔開,保證數字都小於等於1000000。
輸出: 一個數字,表示[A,B]區間內的水仙花數個數
樣例輸入: 100 1000
樣例輸出: 4
答案提示: 100~1000的水仙花數有:153,370,371,407
*/spa
1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 7 int a = 0; 8 int b = 0; 9 int count = 0; 10 11 Scanner s = new Scanner(System.in); 12 String str = s.nextLine(); 13 String[] strArray = str.split(" "); 14 a = Integer.parseInt(strArray[0]); 15 b = Integer.parseInt(strArray[1]); 16 s.close(); 17 if((a < 100 || a > 1000000) || (b < 100 || b > 1000000)) 18 { 19 throw new RuntimeException(); 20 } 21 22 count = getNum(a, b); 23 24 System.out.println(count); 25 } 26 27 public static int getNum(int a, int b) { 28 29 int result = 0; 30 31 for(int i = a; i <= b; i++) 32 { 33 int m = i; 34 int tmp = 0; 35 int value = 0; 36 37 while(0 != m) 38 { 39 40 tmp = m%10; 41 value += Math.pow(tmp, 3); 42 m /= 10; 43 } 44 45 if(value == i) 46 { 47 result++; 48 } 49 } 50 return result; 51 } 52 53 }