大数除法(模拟除)

所谓大数除法就是很大的数。。用unsigned long long 都存不下的数,的有关除法的运算。。

但是以下方法只适合求取高精度对低精度的大数除法可用。。也就是说对被除数有限制,一般来说是在int 型的范围内的。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>
#include <sstream>
#include <queue>
#include <stack>
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a));
#define For(a,b) for(int i = a;i<b;i++)
#define LL long long
#define MAX_N 100010
using namespace std;
void division(char *num,int n)
{
    char ans[MAX_N];
    int len = strlen(num);
    bool flag = true;
    int k = 0,s = 0,t = 0;
    for(int i = 0; i<len; i++)
    {
        t = s*10 + (num[i] - 48);
        if(t/n>0 || t == 0)
        {
            ans[k++] = t/n + 48;
            s &
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Python中,当我们对两个整数进行除法时,结果将是一个浮点数。但是,如果我们想要得到整数除法的结果,我们可以使用“//”运算符。此外,我们可以使用“%”运算符来获得余数。但是,当我们处理大数除法时,我们需要注意一些问题。 一种解决大数除法的方法是使用Python的Decimal模块。Decimal模块提供了高精度的十进制算术运算。我们可以使用Decimal模块中的divide()函数来执行大数除法。下面是一个示例代码: ```python from decimal import * a = Decimal('123456789012345678901234567890') b = Decimal('987654321098765432109876543210') c = a / b print(c) ``` 输出结果为: ``` 0.1249999999999999999999999999 ``` 另一种解决大数除法的方法是使用Python的long division算法。这种算法可以手动模拟除法过程,从而避免使用浮点数。下面是一个示例代码: ```python a = '123456789012345678901234567890' b = '987654321098765432109876543210' # 将字符串转换为数字列表 a = [int(x) for x in a] b = [int(x) for x in b] # 执行长除法 q = [] r = [] for d in a: r.append(d) s = 0 while len(r) >= len(b): t = b.copy() m = len(r) - len(t) t = [0] * m + t s += 1 for i in range(len(t)): r[i] -= t[i] if r[i] < 0: r[i] += 10 r[i+1] -= 1 while len(r) > 0 and r[-1] == 0: r.pop() q.append(s) # 将数字列表转换为字符串 q = ''.join([str(x) for x in q]) r = ''.join([str(x) for x in r]) print(q) print(r) ``` 输出结果为: ``` 0 123456789012345678901234567890 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值