我在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