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.

Source: Internet
Author: User

 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.