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>>A>>B) {cout<<add (b) <<endl; }//while return 0;}
[Huawei Machine Test exercises] 2. Sum of large numbers