高精度
2024-12-21 15:42:01
发布于:江苏
3阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
int a[10005],b[10005],c[10005];
void stoHp(string s,int arr[]){
	arr[0] = s.length();
	for (int i = 1,j = s.length() - 1; i <= arr[0];i++,j--){
		arr[i] = s[j] - '0';
	}
}
void add(int a[],int b[],int c[]){
	c[0] = max(a[0],b[0]);
	for (int i = 1;i <= c[0];i++){
		c[i] = a[i] + b[i] + c[i];
		c[i + 1] = c[i] / 10;
		c[i] %= 10;
	}
	//进位判断
	if (c[ c[0]+1 ] > 0) ++c[0];
	//去前导零 
	while ( c[ c[0] ] == 0 && c[0] > 1 ) --c[0];
}
void print(int arr[]){
	for (int i = arr[0];i >= 1;i--) cout << arr[i];
}
int main(){
	string s1,s2;
	cin >> s1 >> s2;
	stoHp(s1,a);
	stoHp(s2,b);
	add(a,b,c);
	print(c);
	return 0;
}
这里空空如也



有帮助,赞一个