求大佬解答下高精度乘法疑问,万分感谢!
  • 板块学术版
  • 楼主Cells
  • 当前回复3
  • 已保存回复3
  • 发布时间2023/8/4 22:29
  • 上次更新2023/11/3 05:51:13
查看原帖
求大佬解答下高精度乘法疑问,万分感谢!
556908
Cells楼主2023/8/4 22:29

题目描述:


给定多行整数(每行一个)输出它们的积(每个数不大于101位)总行数不超过10
输入: 1 2 3
输出: 6


My Code:

# include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;

string s;

struct longnum{
	int l, f, d[N];
	longnum(){}
	longnum(string a){
		l = a.size();
		if(a[0] == '-'){
			f = -1;
			a[0] = '0';
		}
		else f = 1;
		
		for(int i = 1; i <= l; i ++)
        	d[i] = a[l - i] - '0';
	}
	
	longnum(int a){
		if(a < 0){
			f = -1;
			a = -a;
		}
		
		else f = 1;
		l = 0;
		do d[++ l] = a % 10, a /= 10; while(a);
	}
	
	longnum operator +(longnum oth){
		longnum t;
		t.l = max(l, oth.l);
		for(int i = l + 1; i <= t.l; i ++) d[i] = 0;
		for(int i = oth.l + 1; i <= t.l; i ++) oth.d[i] = 0;
		int num = 0;
		for(int i = 1; i <= t.l; i ++){
			t.d[i] = d[i] + oth.d[i] + num;
			num = t.d[i] / 10;
			t.d[i] %= 10;
		}
	
		if(num) t.d[++ t.l] = num;
		return t;
	}
	
	longnum operator *(longnum oth){
		longnum t;
		t.l = l + oth.l;
		t.f = f * oth.f;
		for(int i = 1; i <= t.l; i ++) t.d[i] = 0;
		for(int i = 1; i <= l; i ++){
			for(int j = 1; j <= oth.l; j ++){
				t.d[i + j - 1] += d[i] * oth.d[j];
			}
		}
		
		int num = 0;
		for(int i = 1; i <= t.l; i ++){
			t.d[i] += num;
			num = t.d[i] / 10;
			t.d[i] %= 10;
		}
		
		while(t.d[t.l] == 0 && t.l > 1) t.l --;
		
		return t;
	}
	
	friend ostream& operator << (ostream &out, longnum a){
		if(a.l == 1 && a.d[1] == 0) a.f = 1;
		if(a.f == -1) cout << "-";
		for(int i = a.l; i >= 1; i --) cout << a.d[i];
		return out;
	}
	
	friend istream& operator >> (istream &in, longnum &a){
		string s;
		in >> s;
		a = s;
		return in;
	}
}A, B;

int main(){
	B = 1;
	while(cin >> A) B = B * A;
	cout << B;
	
	return 0;
}

希望大佬们能帮蒟蒻写一下每个重载定义的注释,解释一下意思和作用,球球了!!!

作为回报,本蒟蒻会关注大佬们的!!!

在线等,急

2023/8/4 22:29
加载中...