高精度乘法求帮助
  • 板块题目总版
  • 楼主molly1212
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/8/26 18:47
  • 上次更新2024/8/26 19:03:31
查看原帖
高精度乘法求帮助
556017
molly1212楼主2024/8/26 18:47

P1303 A*B Problem

#include <bits/stdc++.h>
using namespace std;
string s,t;
struct LLI
{
	int a[5000],len;
	LLI(int x = 0)
	{
		memset(this->a,0,sizeof(this->a));
		this->len = 0;
		while (x)
		{
			this->a[++this->len] = x % 10;
			x /= 10;
		}
	}
	int &operator[](int k)
	{
		return this->a[k];
	}
	void flatten(int k)
	{
		for (int i = 1;i <= k;i++)
		{
			this->a[i+1] += this->a[i]/10;
			this->a[i] %= 10;
		}
		int i = k;
		while (this->a[i] == 0 && i > 1) i--;
		this->len = i;
	}
	void print()
	{
		for (int i = this->len;i >= 1;i--)
		{
			cout << this->a[i];
		}
		cout << endl;
	}
};
LLI operator+(LLI a,LLI b)  //这一段不用,就当注释了
{
	LLI c;
	int size = max(a.len,b.len) + 1;
	for (int i = 1;i <= size;i++)
	{
		c[i] += a[i] + b[i];
	}
	c.flatten(size + 3);
	return c;
}
LLI operator*(LLI a,LLI b)
{
	LLI c;
	for (int i = 1;i <= a.len;i++)
	{
		for (int j = 1;j <= b.len;j++)
		{
			c[i+j-1] = a[i] * b[j];
		}
	}
	c.flatten(a.len+b.len+5);
	return c;
}
int main()
{
	LLI a,b,c;
	cin >> s >> t;
	int lens = s.length(),lent = t.length();
	for (int i = lens-1;i >= 0;i--)
	{
		a[lens-i] = s[i]-'0';
	}
	a.len = lens;
	for (int i = lent-1;i >= 0;i--)
	{
		b[lent-i] = t[i]-'0';
	}
	b.len = lent;
	c = a*b;
	c.print();
	return 0;
}

过不了......

2024/8/26 18:47
加载中...