高精度数除以低精度数

高精度数除以低精度数

一、思想

将读入的被除数用字符串存储,根据除法竖式思想,将一个被除数的每一位分别与另一个数相除。

二、过程

1、读入字符串a1, 与除数b1;
2、将字符串倒序存储并且将每个元素转化为整数类型:

lena = a1.size();
int a[5005] = {};
for (int i = 0; i < lena; i++) {
    a[lena - i] = a1[i] - '0';
}

3、做除法运算

int c[5005], lenc = 1;
for (int i = 1; i <= lena; i++) {
	c[i] = a[i] / b1;
	a[i + 1] += a[i] % b1 * 10;
}

4、处理前导0,倒序输出

while (lenc < lena && c[lenc] == 0) lenc++;
while (lenc <= lena) printf("%d", c[lanc--]);

三、函数代码

#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
string dlv(string a1, int b1) {
	string c1;
	int a[10005] = {}, c[10005] = {};
	int lena = a1.size(), lenc = 1;
	for (int i = 0; i < lena; i++) {
		a[i + 1] = a1[i] - '0';
	}
	for (int i = 1; i <= lena; i++) {
		c[i] = a[i] / b1;
		a[i + 1] += a[i] % b1 * 10;
	}
	while (lenc < lena && c[lenc] == 0) lenc++;
	while (lenc <= lena) c1 += c[lenc++] + '0';
	return c1;
}
int main() {
	string a;
	int b;
	cin >> a;
	scanf("%d", &b);
	cout << dlv(a, b);
	return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值