Packageinterview_10_10;Importorg.junit.Test; Public classT1 {/*** If the system is going to use a very large integer (over a long length range), you should design a data structure to store this very large number and design an algorithm for large integer addition operations. */@Test Public voidtest1 () {String number1= "4324328732789"; String number2= "2383244324324325898"; String result=Doadd (Number1, number2); SYSTEM.OUT.PRINTLN (result); } /** * @paramNUM1 *@paramnum2 *@returnreturns the result * 1. The left side of the calculation is a few 0 * 2. Start from the right to add*/ Public Staticstring Doadd (String num1, String num2) {string str= ""; intLena =num1.length (); intLenB =num2.length (); intMaxLength = Lena > LenB?Lena:lenb; intMinLength = Lena < LenB?Lena:lenb; String strtemp= ""; for(inti = (maxlength-minlength); i > 0; i--) {//to calculate the left side need to fill a few 0strtemp + = 0; } if(maxlength = = Lena) {//left 0num2 = strtemp +num2; } Else{NUM1= strtemp +NUM1; } intJW = 0; for(inti = (maxlength-1); I >= 0; i--) { inttemp = 0; intNumber1 =integer.valueof (string.valueof (Num1.charat (i))); intNumber2 =integer.valueof (string.valueof (Num2.charat (i))); if(Number1 + number2 + JW > && i! = 0) {Temp= number1 + number2 + jw-10; } Else{Temp= number1 + number2 +JW; } STR= string.valueof (temp) +str; } returnstr; }}
If the system is going to use very large integers (over a long length range), you can design a data structure to store this very large number and design an algorithm to implement the extra large integer addition operation.