高精度加法
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<string.h> 6 using namespace std; 7 typedef long long ll; 8 char aa[100010]; 9 char bb[100010]; 10 int a[1000010], b[100010]; 11 int c[10000010]; 12 void add(int a[], int b[]) 13 { 14 int l = max(a[0], b[0]); 15 for (int i = 1; i <= l; i++) 16 { 17 c[i] = a[i] + b[i]; 18 } 19 l++; 20 for (int i = 1; i <= l; i++) 21 { 22 if (c[i] > 9) 23 { 24 c[i + 1]++; 25 c[i] = c[i] - 10; 26 } 27 } 28 while (c[l] == 0 && l > 1) 29 { 30 l--; 31 } 32 for (int i = l; i >= 1; i--) 33 { 34 cout << c[i]; 35 } 36 cout << endl; 37 } 38 int main() 39 { 40 scanf("%s", &aa); 41 scanf("%s", &bb); 42 int l1 = strlen(aa); 43 int l2 = strlen(bb); 44 a[0] = l1; 45 b[0] = l2; 46 for (int i = l1; i >=1; i--) 47 { 48 a[i] = aa[l1 - i] - '0'; 49 } 50 for (int i = l2; i >= 1; i--) 51 { 52 b[i] = bb[l2 - i] - '0'; 53 } 54 add(a, b); 55 return 0; 56 }