首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

硕大无比数相加,相乘

2012-06-26 
超大数相加,相乘超出long上限的大数相加,相乘算法。public class StringCompute {private static final cla

超大数相加,相乘
超出long上限的大数相加,相乘算法。

public class StringCompute {private static final class instanceClass {public static final StringCompute instance = new StringCompute();}private static StringCompute getInstance() {return instanceClass.instance;}private StringCompute() {}/** * [方法功能说明] *  * @param args */public static void main(String[] args) {long start = System.currentTimeMillis();StringCompute.getInstance().multiply("4545687745456468777878787878787", "46465464646465445456656465");System.out.println((System.currentTimeMillis() - start)+"[ms]");start = System.currentTimeMillis();System.out.println(StringCompute.getInstance().add("4545687745456468777878787878787", "46465464646465445456656465"));System.out.println((System.currentTimeMillis() - start)+"[ms]");}/** *  * [超大数相加] *  * @param a * @param b * @return */private String add(String a, String b) {StringBuilder c = new StringBuilder("");char[] aArray = a.toCharArray();char[] bArray = b.toCharArray();int upper = 0; // 进位int length = aArray.length < bArray.length ? aArray.length: bArray.length;char[] maxArray = aArray.length > bArray.length ? aArray : bArray;int i = 0;for (; i < length; i++) {int temp = Integer.parseInt("" + (aArray[aArray.length - i - 1]))+ Integer.parseInt("" + bArray[bArray.length - i - 1])+ upper;upper = temp / 10;c.append(temp % 10);}if (aArray.length != bArray.length) {// c.append(maxArray[maxArray.length - i -1]+upper);for (int j = (maxArray.length - i - 1); j >= 0; j--) {int temp = Integer.parseInt("" + (maxArray[j])) + upper;upper = temp / 10;c.append(temp % 10);}}if (upper != 0) {c.append(upper);}return c.reverse().toString();}private void multiply(String p1, String p2) {// ;int ASCII = 48;char[] result = new char[p1.length() + p2.length()];for (int i = 0; i < result.length; i++) {result[i] = '0';}char ctemp = ' ';//逆序char[] pc1 = p1.toCharArray();for (int i = 0; i < pc1.length / 2; i++) {ctemp = pc1[i];pc1[i] = pc1[pc1.length - 1 - i];pc1[pc1.length - 1 - i] = ctemp;}//逆序char[] pc2 = p2.toCharArray();for (int i = 0; i < pc2.length / 2; i++) {ctemp = pc2[i];pc2[i] = pc2[pc2.length - 1 - i];pc2[pc2.length - 1 - i] = ctemp;}int temp = 0;// 临时结果int step = 0;// 进位int time = 0;// 次数int bit = 0;// 位数for (char c1 : pc1) {time = bit;for (char c2 : pc2) {temp = (c1 - ASCII) * (c2 - ASCII);temp = temp + result[time] - ASCII;if (temp > 9) {// 进位int i = 0;do {result[time + i] = (char) (temp % 10 + ASCII);step = (temp - temp % 10) / 10;i++;temp = result[time + i] - ASCII + step;} while (temp > 9);result[time + i] = (char) (temp + ASCII);} else {result[time] = (char) (temp + ASCII);}time++;}bit++;}System.out.println("##############################");//System.out.print(p1 + " x " + p2 + " = ");for (int i = result.length - 1; i >= 0; i--) {System.out.print(result[i]);}System.out.println();System.out.println("##############################");}}

热点排行