我在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;
}
}
這將很容易推廣到任何單個數字乘法。 – CandiedOrange 2015-04-04 01:59:02
真正有用的咖喱。非常感謝。我從來不會想出如此簡單而又精彩的事物! – Ian 2015-04-04 02:02:57
@Ian這是自動執行你必須手動完成的操作。大多數計算機算法早在計算機出現之前就存在;) – 2015-04-04 02:13:47