PS:這是我剛作的一道題,題目不難,滿分60,得分40,你們看看哪裏有問題,歡迎提意見,感謝!java
/*函數
* 題目:Excel表格縱列字母數字轉換spa
* 描述: 在Excel中列的編號爲A-Z,AA-AZ,BA-BZ.....CZB.....,請實現一個函數要求知足如下功能,將編號進行修改,code
* 如A-Z爲1-26,而後翻轉爲兩位,AA爲27,AB爲28。。。。。ZZ爲702,而後翻轉爲3位,AAA=703,AAB=704。。。blog
輸入的字符串最長只有4。內存
題目類別: 字符串字符串
難度: 初級class
分數: 60import
運行時間限制: 無限制im
內存限制: 無限制
階段: 應聘考試 輸入: 1.輸入爲字符串,其中全部字母均爲大寫(不做爲異常輸入校驗點),字母個數不超過4個(代碼要校驗)。
輸出: 輸出運算結果
如:輸入AA對應的結果是27 樣例輸入: AB 樣例輸出: 28
*/
1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 7 int num = 0; 8 int len = 0; 9 10 Scanner s = new Scanner(System.in); 11 String str = s.nextLine(); 12 s.close(); 13 14 len = str.length(); 15 if(len <= 0 || len >4) 16 { 17 return; 18 } 19 for(int i = 0; i < len; i++) 20 { 21 char ch = str.charAt(i); 22 if(ch < 'A' || ch > 'Z') 23 { 24 return; 25 } 26 } 27 28 num = strToNum(str, len); 29 30 System.out.println(num); 31 32 } 33 34 private static int strToNum(String str, Integer len) { 35 36 int num = 0; 37 int result = 0; 38 39 for(int i = 0; i < len; i++) 40 { 41 char ch = str.charAt(len - i - 1); 42 num = (int)(ch - 'A' + 1) ; 43 num *= Math.pow(26, i); 44 result += num; 45 } 46 return result; 47 } 48 49 }