我想问问我这个高精度乘法为什么会出现奇怪的符号???(C++)
  • 板块学术版
  • 楼主helpcyg
  • 当前回复11
  • 已保存回复11
  • 发布时间2020/7/25 21:19
  • 上次更新2023/11/6 22:17:11
查看原帖
我想问问我这个高精度乘法为什么会出现奇怪的符号???(C++)
327288
helpcyg楼主2020/7/25 21:19

直接来代码:

#include<iostream>
using namespace std;
#define str string 
str p(str a,str b){
	str ans = "";
	int up = 0;
	if(a.length() < b.length()){
		swap(a,b);
	}
	while(b.length() < a.length()){
		b = '0' + b;
	}
	for(int i = b.length() - 1;i >= 0;i--){
		int tmp = char(a[i] - '0') + char(b[i] - '0') + up;
		up = tmp / 10;
		tmp %= 10;
		char t = char(tmp + '0');
		ans = t + ans;
	}
	if(up != 0){
		char u = up + '0';
		ans = u + ans;
	}
	return ans;
}

int main(){
	str a,b,times = "";
	cin>>a>>b;
	if(a.length() < b.length()){
		swap(a,b);
	}
	int la = a.length(),lb = b.length();
	int up = 0;
	for(int i = la - 1;i >= 0;i--){
		str t = "",t_ans = "";
		for(int j = lb - 1;j >= 0;j--){
			int tmp = (a[i] - '0') * (b[j] - '0') + up;
			up = tmp / 10;
			tmp %= 10;
			for(int a = 0;a < la - 1 - i;a++) tmp *= 10;
			char ttt = tmp + '0';
			string t(1,ttt);
			t_ans = t + t_ans;
		}
		times = p(t_ans,times);
	}
	cout<<times;
	return 0;
}

2020/7/25 21:19
加载中...