高精度加法
https://www.luogu.org/problemnew/show/P1601
1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 char a1[100],b1[100]; 6 int a[100],b[100],c[100]; 7 int lenc = 0; 8 void primer(char *a1,char *b1) 9 { 10 //把a1,b1转成字符串倒序存入a,b,下标从1到N 11 memset(a,0,sizeof(a)/sizeof(a[0])); 12 memset(b,0,sizeof(b)/sizeof(b[0])); 13 14 int lena = strlen(a1); 15 int lenb = strlen(b1); 16 int r = 0;//进位 17 for(int i = 0;i<lena;++i) 18 { 19 a[lena-i] = a1[i]-'0';//'0'=48 20 } 21 for(int i = 0;i<lenb;++i) 22 { 23 b[lenb-i] = b1[i]-'0';//'0'=48 24 } 25 //模拟加法 26 lenc = max(lena,lenb); 27 for(int i = 1;i<=lenc;++i) 28 { 29 c[i] = a[i]+b[i]+r; 30 r = c[i]/10; 31 c[i] %= 10; 32 } 33 if(r!=0) 34 { 35 c[++lenc] = r; 36 } 37 } 38 int main() 39 { 40 cin >> a1>>b1; 41 primer(a1,b1); 42 //倒序输出 43 for(int i = lenc;i>=1;--i) 44 { 45 cout << c[i]; 46 } 47 cout << endl; 48 return 0; 49 }