2015-04-04 86 views
0

我在java中編寫了我自己的大整數類,沒有導入,並且需要一種將字符串表示的任意大小數加倍的方法。我現在的代碼現在可以運行,但一旦數字變得越來越大,開始需要很長時間。我基本上創建了兩個數組:主數組和倒數數組,它們都以相同的方式開始。然後,我運行一個while循環並向上遞增主數組並向下遞增倒數數組。當倒數數組達到「0」時,我終止循環,結果是一個新數組,新數字的大小加倍。那麼當然,我有if語句來檢查數組是否需要更改十位數等....這是我的...有什麼辦法可以使它更高效和快速?雙十進制字符串

public static String doubleDecimalString (String main) { 
    String countdown = main;  
    String finalBuild = ""; 
    boolean runLoop = true; 

    //if zero is supplied, skip all the nonsense and just return 0 
    //else, loop through and find the true double 

    //was having trobule getting single digits to double correctly so i had to hard code this for now. 
    if (main.equals("0")) { 
     return main; 
    } else if (main.equals("5")) { 
     return "10"; 
    } else if (main.equals("6")) { 
     return "12"; 
    } else if (main.equals("7")) { 
     return "14"; 
    } else if (main.equals("8")) { 
     return "16"; 
    } else if (main.equals("9")) { 
     return "18"; 
    } else { 
     //Array for ORIGINAL NUMBER 
     int[] mainPiece = new int[main.length()+2]; 
     int arrayLength = mainPiece.length; 

     for (int i = 0; i < main.length(); i++) { 
      mainPiece[i+2] = Integer.parseInt(main.substring(i, i+1)); 
     } 
     mainPiece[0] = -1; 
     mainPiece[1] = -1; 

     //Array for COUNTDOWN NUMBER 
     int[] countdownPiece = new int[main.length()+2]; 

     for (int i = 0; i < main.length(); i++) { 
      countdownPiece[i+2] = Integer.parseInt(main.substring(i, i+1)); 
     } 
     countdownPiece[0] = -1; 
     countdownPiece[1] = -1; 

     while ( runLoop) { 

      //Increment and decrement the two arrays 
      mainPiece[arrayLength-1] += 1;  
      countdownPiece[arrayLength-1] -= 1;   

      //UPDATE MAIN ARRAY 
      if ( mainPiece[arrayLength-1] == 10) { 
       for (int x = arrayLength-1; x > 0; x--) { 

        if ((mainPiece[x] == 10) && (mainPiece[x-1] != 9)) { 
         mainPiece[x] = 0; 
         mainPiece[x -1] += 1; 
        } else if ((mainPiece[x] == 10) && (mainPiece[x-1] == 9)) { 
         mainPiece[x] = 0; 
         mainPiece[x -1] += 1;  
         x = arrayLength; 
        } 
        if ((mainPiece[2] == 10)) { 
         mainPiece[1] = 1; 
         mainPiece[2] = 0; 
        } 

       } 
      } // end main array 

      //UPDATE SIDE ARRAY 
      if ( countdownPiece[arrayLength-1] == -1) { 
       for (int x = arrayLength-1; x > 0; x--) { 

        if ((countdownPiece[x] == -1) && (countdownPiece[x-1] > 0) && (x > 1) ) { 
         countdownPiece[x] = 9; 
         countdownPiece[x -1] -= 1; 
        } else if ((countdownPiece[x] == -1) && (countdownPiece[x-1] == 0) && (x > 1)) { 
         countdownPiece[x] = 9; 
         countdownPiece[x -1] -= 1;  
         x = arrayLength; 
        } 

       } 
      } //end side array 


      //tests whether the pieces need to be switched to -1 for scanning 
      for (int x = 0; x < arrayLength - 1; x++) { 
        if ((countdownPiece[x] == -1) && (countdownPiece[x+1] == 0)) { 
         countdownPiece[x+1] = -1; 
        } 
      } 

      //if the side array has reached "0" then the loop will stop and the main array will return the new doubled value 
      if ((countdownPiece[arrayLength-1] == -1) && (countdownPiece[arrayLength-2] == -1)) { 
       break; 
      } 

     } //end while loop 

      //transform array into string 
      finalBuild = "";  
      for (int T = 0; T < arrayLength; T++) { 
       finalBuild += (mainPiece[T] != -1) ? mainPiece[T] : ""; 
      } 
       return finalBuild; 
    }   

} 

回答

2

如何像這樣(它基本上由兩個做乘法,佔載):

private String doubleNumber(String number) 
{ 
    int doubleDig = 0; 
    int carry = 0; 

    StringBuilder sb = new StringBuilder(); 
    for (int i = number.length() - 1; i >= 0; --i) 
    { 
     char c = number.charAt(i); 
     int origNum = Character.getNumericValue(c); 
     doubleDig = origNum * 2 + carry; 
     carry = doubleDig/10; 
     doubleDig = doubleDig % 10; 

     sb.append(doubleDig); 
    } 
    if (carry > 0) 
    { 
     sb.append(carry); 
    } 

    return sb.reverse().toString(); 
} 

顯然,這隻能處理整數。

+0

這將很容易推廣到任何單個數字乘法。 – CandiedOrange 2015-04-04 01:59:02

+0

真正有用的咖喱。非常感謝。我從來不會想出如此簡單而又精彩的事物! – Ian 2015-04-04 02:02:57

+1

@Ian這是自動執行你必須手動完成的操作。大多數計算機算法早在計算機出現之前就存在;) – 2015-04-04 02:13:47

0

我會使用StringBuilder或List來建立你的doubled值。

使用隨身攜帶變量來存儲運送量並初始化爲0

開始在至少顯著位,雙位數字,並添加進。

然後設置carrydigit/10,那麼digitdigit % 10

追加digit到您的製造商或列表。

循環遍歷所有數字後,檢查carry是否大於0並在需要時追加。

反轉StringBuilder或列表並加入,你有你的答案。

public class Doubler { 
    public static void main(String[] args) { 
     System.out.println(doubleDec("9123123123087987342348798234298723948723987234982374928374239847239487.23233099")); 
    } 

    public static String doubleDec(String dec) { 
    StringBuilder builder = new StringBuilder(); 
    int carry = 0; 
    for (int i = dec.length() - 1; i > -1 ; i--) { 
     char charDigit = dec.charAt(i); 
     if (charDigit == '.') { 
     builder.append(charDigit); 
     } else { 
     int digit = Character.getNumericValue(charDigit); 
     if (digit == -1) { 
      throw new IllegalStateException("Invalid character in decimal string."); 
     } 
     digit = digit * 2 + carry; 
     carry = digit/10; 
     digit = digit % 10; 
     builder.append(digit); 
     } 
    } 
    if (carry != 0) { 
     builder.append(carry); 
    } 
    return builder.reverse().toString(); 
    } 
} 

// 18246246246175974684697596468597447897447974469964749856748479694478974.46466198