[Huawei Machine Test exercises] 2. Sum of large numbers

Source: Internet
Author: User

Topic two [large number sum]

Describe:
Given two very large positive integers a and B, the number of digits is between 50 and 100. Seeking c=a+b;

Title Category: String
Difficulty: Intermediate
Operating time limit: 10Sec
Memory Limit: 128MByte
Stage: Pre-employment practice
Input:
Because A and B are large, from high to low, enter two lines of numbers a and B as strings. The number of bits A and B is between 50 and 100.

Output:
Outputs a line as a string, representing the and of A and B.

Sample input:
11111111111111111111111111111111111111111111111111
22222222222222222222222222222222222222222222222222

Sample output:
33333333333333333333333333333333333333333333333333

Code

/* ---------------------------------------* Date: 2015-06-28* sjf0115* time: 2014* title: Large Number Summation * Source: Huawei on-Machine---------------- -------------------------*/#include <iostream>#include <string>using namespace STD;stringADD (stringAstringb) {intSize1 = A.size ();if(Size1 = =0){returnb }//if    intSize2 = B.size ();if(Size2 = =0){returnA }//if    //Rounding    intc =0, Num1,num2,sum;stringresult =""; for(inti = size1-1, j = size2-1; I >=0|| J >=0|| C >0;----,--j) {num1 = i >=0? A[i]-' 0 ':0; num2 = J >=0? B[J]-' 0 ':0;        sum = num1 + num2 + C; c = SUM/Ten; Result.insert (Result.begin (), Sum%Ten+' 0 '); }//for    returnResult;}intMain () {stringAstringb while(Cin&GT;&GT;A&GT;&GT;B) {cout<<add (b) <<endl; }//while    return 0;}

[Huawei Machine Test exercises] 2. Sum of large numbers

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.