package interview_10_10;import org.junit.Test;public class T1 {/*** 如果系统要使用超大整数(超过long长度范围),请你设计一个数据结构来存储这种超大型数字以及设计一种算法来实现超大整数加法运算)。*/@Testpublic void test1() {String number1 = "4324328732789";String number2 = "2383244324324325898";String result = doAdd(number1, number2);System.out.println(result);}/*** @param num1* @param num2* @return 返回结果* 1.计算小的那个左边需要补几个0* 2.从右边开始一个个的开始相加*/public static String doAdd(String num1, String num2) {String str = "";int lena = num1.length();int lenb = num2.length();int maxlength = lena > lenb ? lena : lenb;int minlength = lena lena : lenb;String strtemp = "";for (int i = (maxlength - minlength); i > 0; i--) { // 计算左边需要补几个0strtemp += 0;}if (maxlength == lena) { // 左边补零num2 = strtemp + num2;} else {num1 = strtemp + num1;}int jw = 0;for (int i = (maxlength - 1); i >= 0; i--) {int temp = 0;int number1 = Integer.valueOf(String.valueOf(num1.charAt(i)));int number2 = Integer.valueOf(String.valueOf(num2.charAt(i)));if (number1 + number2 + jw > 10 && i != 0) {temp = number1 + number2 + jw - 10;} else {temp = number1 + number2 + jw;}str = String.valueOf(temp) + str;}return str;}
}