高精度算法

高精度加法

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 505;
int a[N], b[N], c[N];
string strA, strB;
int lena, lenb, lenc;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> strA >> strB;
	lena = strA.size();
	lenb = strB.size();
	for (int i = 0; i < lena; ++i)//倒序储存防止位数不一样,难以计算
		a[lena - i] = strA[i] - '0';
	for (int i = 0; i < lenb; ++i)
		b[lenb - i] = strB[i] - '0';
	lenc = max(lena, lenb) + 1;
	for (int i = 1; i <= lenc; ++i)//模拟竖式计算
	{
		c[i] += a[i] + b[i];
		c[i + 1] += c[i] / 10;
		c[i] %= 10;
	}
	while (!c[lenc] && lenc > 1)//因为是倒序计算的,所以可能会有前导零,要删除
		lenc--;
	for (int i = lenc; i >= 1; --i)//倒序输出
		cout << c[i];
	return 0;
}

高精度减法

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 10089;
LL a[N], b[N], c[N];
char s1[N], s2[N], s3[N];
LL lena, lenb, lenc;

//判断两个数的大小, s1>=s2 返回true, 否则返回false
bool compare(char s1[], char s2[])
{
	int u = strlen(s1), v = strlen(s2);
	if (u != v)
		return u > v;
	for (int i = 0; i < u; ++i)
		if (s1[i] != s1[i])
			return s1[i] > s2[i];
	return true;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	scanf("%s", s1);
	scanf("%s", s2);
	int flag = 0;
	if (!compare(s1, s2))//如果s1<s2, 则交换两个数
	{
		flag = 1;
		strcpy(s3, s1);
		strcpy(s1, s2);
		strcpy(s2, s3);
	}
	lena = strlen(s1), lenb = strlen(s2);
	for (int i = 0; i < lena; ++i)
		a[lena - i] = s1[i] - '0';
	for (int i = 0; i < lenb; ++i)
		b[lenb - i] = s2[i] - '0';
	lenc = max(lena, lenb);
	for (int i = 1; i <= lenc; i++)
	{
		if (a[i] < b[i])
		{
			a[i + 1]--;
			a[i] += 10;
		}
		c[i] = a[i] - b[i];
	}
	while (!c[lenc] && lenc > 1)//删除前导零
		lenc--;
	if (flag == 1)//如果差值为负数,输出负号
		cout << "-";
	for (int i = lenc; i >= 1; --i)
		cout << c[i];
	return 0;
}

高精度乘法

​
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 5000;
LL a[N], b[N], c[N];
char s1[N], s2[N], s3[N];
LL lena, lenb, lenc;

int main()
{
	scanf("%s", s1);
	scanf("%s", s2);
	lena = strlen(s1), lenb = strlen(s2);
	for (int i = 0; i < lena; ++i)
		a[lena - i] = s1[i] - '0';
	for (int i = 0; i < lenb; ++i)
		b[lenb - i] = s2[i] - '0';
	lenc = lena + lenb;
	for(int i=1; i<=lena;i++)//模拟乘法竖式
		for (int j = 1; j <= lenb; j++)
		{
			c[i + j - 1] += a[i] * b[j];
			c[i + j] += c[i + j - 1] / 10;
			c[i + j - 1] %= 10;
		}
	while (!c[lenc] && lenc > 1)
		lenc--;
	for (int i = lenc; i >= 1; --i)
		cout << c[i];
	return 0;
}

​

高精度除法(高精度除以低精度(除数很小)):逐位试商法

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 5005;
char s1[N];
LL b, c[N], x, a[N], lena, lenc;

int main()
{
	cin >> s1 >> b;//读入被除数和除数
	x = 0;
	lena = strlen(s1);
	for (int i = 1; i <= lena; ++i)
		a[i] = s1[i - 1] - '0';//因为是从高位开始除,所以不需要进行倒序转置
	for (int i = 1; i <= lena; ++i)//模拟除法竖式
	{
		c[i] = (x * 10 + a[i]) / b;
		x = (x * 10 + a[i]) % b;
	}
	lenc = 1;
	while (c[lenc] == 0 && lenc < lena)
		lenc++;//删除前导零
	for (int i = lenc; i <= lena; ++i)//从非零位开始输出
		cout << c[i];
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值