NOI1.6 10: large integer addition

Calculated on high-precision really Kengren ah ==

Wherein the adder is said to be the most simple, nothing more than the array size, carry, zero problems preamble (for addition).

topic:


 

10: large integer addition

Total time limit: 1000ms Memory Limit: 65536kB
description

Seeking two less than 200 bit non-negative integers. Note that the results may exceed 200

Entry
There are two rows of not more than 200 is a non-negative integer, may have extra leading zero .
Export
Line, i.e., the added result. The results in can not have extra leading 0 , that is, if the result is 342, you can not output to 0342.
Sample input
22222222222222222222
33333333333333333333
Sample Output
55555555555555555555
source
Programming Internship 2007

Conventional input sample (Note: Please ensure sufficient digits):

999999999999999
1
1000000000000000

 

Special input sample:

Enter the test :( 1 leading 0)

0000001000000
0000001000000

Output 1:

2

 

Enter the test to retain the last 2 :( 0)

000000000000
000000000000

Output 2:

0

 

Evil nine points ...

Ideas + solution to a problem

1  / * 
2      high-precision adder Note 
 3      * input preamble may also be 0, when the output of this program can not go only 0. concentrated to a zero.
4      * input 200, output 201 may have (Most Significant bit) made slightly larger so that the memory always good.
5      * to zero to focus attention on output, if a 0 + 0 = 0 it is not on no output yet, so additional
 6        to determine the answer only if zero. 
. 7  * / 
. 8  
. 9  
10 # the include <stdio.h>
 . 11 # the include < String .h>
 12 is # the include <the iostream>
 13 is  
14  the using  namespace STD;
 15  
16  int main ()
 . 17  {
 18 is      char A1 [ 250 ], B1 [ 250];
19     int a[250], b[250], c[250], lena, lenb, lenc, i, x;
20     
21     memset(a, 0, sizeof(a));
22     memset(b, 0, sizeof(b));
23     memset(c, 0, sizeof(c));
24     
25     cin.getline(a1, 250);
26     cin.getline(b1, 250);
27     
28     lena = strlen(a1);
29     lenb = strlen(b1);
30     
31     for(int i = 0; i <= lena-1; i++)
32     {
33         a[lena-i] = a1[i] - '0';
34     }
35     for(int i = 0; i <= lenb-1; i++)
36     {
37         b[lenb-i] = b1[i] - '0';
38     }
39     
40     lenc = 1;
41     x = 0;    //进位 
42     
43     while(lenc <= lena||lenc <= lenb)
44     {
45         c[lenc] = a[lenc] + b[lenc] + x;
46         
47         x = c[lenc]/10;
48         c[lenc] %= 10;
49         lenc++;
50     }
51     
52     c[lenc] = x;
53     
54     if(c[lenc] == 0)
55     {
56         lenc --;
57     }
58     
59      BOOL ISSTART = to false ;
 60      for ( int I = lenc; I> = . 1 ; i-- )
 61 is      {
 62 is          IF ((ISSTART || C [I]) == || I . 1 ) // If the last bit, and 0 is also the output 
63 is          {
 64              COUT << C [I];
 65          }
 66              
67          ISSTART ISSTART = || C [I];
 68      }
 69      return  0 ;
 70 }

 

Guess you like

Origin www.cnblogs.com/serverror/p/12585429.html