騰訊武漢站一面算法題

在excel中有如下對的關係

A  B C ......Z   AA.....AZ   BA....BZ............AAA................ZZZ..............

1..............26  27......52  53.....................703.................................

編寫函數 fun(String  str ) 輸入第一行中的任意一個字符串,輸出對應的整數值。

第一行的字母(既excel表格中的列名)和第二行的數值(既對應的列數)是26進制和10進制的對應關係。

下面是自己寫了個更好點的代碼,希望大家共同商議.

 

protected static  ArrayList<BigInteger> table=new ArrayList<BigInteger>();
     static{
   table.add(BigInteger.valueOf(0));
      }

public static BigInteger fun(String strT){
  int lon=strT.length();
  BigInteger ResultT=(BigInteger)table.get(0);
  for (int i = 0; i <lon; i++) {
   char cha = strT.charAt(i);
         ResultT =ResultT.add(BigInteger.valueOf((long) (Math.pow(26, lon-i-1) * ((int) cha - 64))));
   table.add(ResultT); 
  }
  return  table.get(strT.length());
 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章