C # implements large number additions

Source: Internet
Author: User

In C #, we often need to represent integers. However, in the basic data type of C #, the largest long can only represent a number between 9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.

If we need to represent a larger number, we need to use a certain algorithm to do it.

This time, I'll share the C # #的大数运算之加法 with you.

The code considers only positive integer additions.

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceConsoleApplication18 {9     class ProgramTen     { One         Static voidMain (string[] args) A         { -Console.WriteLine ("Please enter the first addend"); -             stringOnenum =console.readline (); theConsole.WriteLine ("Please enter a second addend"); -             stringTwonum =console.readline (); -  -             stringresult =Twobignumadd (Onenum, twonum); + Console.WriteLine (result); -         } +  A         Static stringTwobignumadd (stringAstringb) at         { -             intK =0; -list<int> array =Newlist<int>(); -list<int> one =Newlist<int>(); -list<int> =Newlist<int>(); -  in             //two numbers are processed into a string of the same length, and a short small number is preceded by 0 -              for(inti =0; I < (a.length > B.length a.length:b.length); i++) to             { +                 if(I >=a.length) -One. Insert (I-a.length,0); the                 Else *One. ADD (int. Parse (A[i]. ToString ())); $                 if(I >=b.length)Panax NotoginsengBoth. Insert (I-b.length,0); -                 Else theBoth. ADD (int. Parse (B[i]. ToString ())); +             } A  the             //the array collection is used to store the sum of the sums, so the maximum length is only 1 longer than the maximum length, and the first 0 +              for(inti =0; I <= (a.length > B.length a.length:b.length); i++) -             { $Array. ADD (0); $             } -  -             //from low to high, each bit starts to add, if add >=10 1 to take the remainder the              for(inti = (A.length > B.length a.length:b.length)-1; I >=0; i--) -             {WuyiArray[i +1] + = (One[i] + two[i])%Ten; theK = (One[i] + two[i])/Ten; -  WuArray[i] + =K; -             } About  $             //if the first is 0, the removal -             if(array[0] ==0) -             { -Array. RemoveAt (0); A             } +  the             //converts a collection into a string return -StringBuilder result =NewStringBuilder (); $              for(inti =0; I < array. Count; i++) the             { the result. Append (Array[i]); the             } the             returnresult. ToString (); -         } in     } the}

C # implements large number additions

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.