蓝桥题目:B-29、高精度加法

蓝桥题目:B-29、高精度加法_第1张图片

#include 
#include 

using namespace std;

int* stoi(int* a, string s, int l){
	for(int i = 0; i < l; ++i){
		a[i] = s[l-1-i] - '0';
	}
	return a;
}

int main(){
	string s1, s2;
	cin >> s1 >> s2;
	
	int lth1 = s1.length(), lth2 = s2.length();
	int maxl = max(lth1, lth2);
	int a[101]={}, b[101]={}, sum[101]={}, temp = 0;
	
	stoi(a, s1, lth1);
	stoi(b, s2, lth2);
	
	for(int i = 0; i <= maxl; ++i){
		sum[i] = (temp + a[i] + b[i]) % 10;
		temp = (temp + a[i] + b[i]) / 10;
	}
	if(sum[maxl] == 0){
		maxl--;
	}
	
	for(int i = maxl; i >= 0; --i){
		cout << sum[i];
	}
	
	return 0;
}

你可能感兴趣的:(Lanqiao)