Algorithm---Big Data addition

Source: Internet
Author: User

Start blogging for the first time to publish an algorithmic blog. Know half of the algorithm exam is used c,c++, because the senior began to this year to graduate work to now has been engaged in C # development, C + + used very little. Linked lists, pointers also know only one concept. Not used to be proficient. So the subsequent update of the algorithm problem I was based on C # syntax. The algorithm mainly embodies the idea of solving problems. As with the topic, this algorithm mainly realizes the big data addition.

Problem Solving Ideas:

1. Store big data in a linked list, stored in C # with list<int>, each node representing the number of each bit. {1,2,3,4,5} = "12345 and {9,6,5,9,5} =" 96595 (c,c++ need to implement the linked list function by hand.) )

2. Add each one of the data. Do not carry the rounding operation. {10,8,8,13,10}

3. Traverse the collection to implement a carry operation that is greater than or equal to 10. {1,0,8,9,4,0}

Code is as follows:

UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;UsingSystem.Text;UsingSystem.Threading.Tasks;Namespacebignumadd{PublicClassprogram {PublicStaticvoid Main (String[] args) {list<int> NUM1 =New list<Int> () {1,2,3,4,5}; list<Int> num2 =New list<Int> () {9,6,5,9,5}; list<int> sum =Addtwonum (NUM1, num2); Console.WriteLine (String. Join ("", sum)); Console.readkey (); }///<summary>///Add two numbers///</summary>///<param name= "NUM1" ></param>///<param name= "Num2" ></param>///<returns></returns>PrivateStatic list<Int> Addtwonum (list<Int> NUM1, list<Int>NUM2) {list<Int> Longnum =Null//Find the long number list<Int> Lessnum =Null//Find the short numberif (NUM1. Count () >=Num2. Count ()) {Longnum =NUM1; Lessnum =num2; }Else{Longnum =num2; Lessnum =NUM1; }//Each location data is added, corresponding to the previous thinking analysis of the second articlefor (int index =0; Index < Lessnum.count (); index++) {Longnum[longnum.count ()-1-index] + = Lessnum[lessnum.count ()-1-Index]; } checknum (Longnum);//Iterating through the collection implements the Carry function. The third line to the previous ideaReturnLongnum; }///<summary>///Carry with each location data greater than or equal to 10///</summary>///<param name= "Bignum" >Large numbers</param>PrivateStaticvoid Checknum (list<Int>Bignum) {for (int index = Bignum.count ()-1; Index >=1; index--) {if (Bignum[index] >=10) {bignum[index- 1] + = Bignum[index]/ 10; //Carry bignum[index] = bignum[index]% 10; // seek remainder } if (bignum[0] > ) {Bignum.insert (0, bignum[0]/ ten); b ignum[1] = bignum[1]% ;           }}} 

Algorithm---Big data addition

Related Article

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.