高精度除法:高精度除以低精度

本编仅限于除数是低精度的情况!!!

基本思想
一位数一位数地与除数相除,余数乘10加入下一位上

举例说明
148除以6
首先用1除以6,得0,余1
再用(1x10+4)除x以6,得2,余2(1是上一位的余数,4是这一位上的数)
再用(2x10+8)除以6,得4,余4(2是上一位的余数,8是这一位的数)
所以结果为024,即24

see the code

#include <bits/stdc++.h>

using namespace std;

int main()
{
    char a[100];//被除数
    int b;//除数
    cin >> a;
    cin >> b;
    int cha[100];
    int l=strlen(a);
    for(int i=0;i<l;i++)//被除数的每一位数转换为int型
    {
        cha[l-i-1]=a[i]-48;
    }
    int ans[100];
    int x=0;
    int m=l-1;
    for(int i=l-1;i>=0;i--)//每一位计算
    {
        ans[i]=(cha[i]+x*10)/b;
        x=(cha[i]+x*10)%b;
    }
    while(!ans[m]&&m>=1)//确定结果的位数
        m--;
    for(int i=m;i>=0;i--)//输出结果
    {
        cout << ans[i];
    }
    cout << endl ;
    return 0;
}

发布了44 篇原创文章 · 获赞 13 · 访问量 2345

猜你喜欢

转载自blog.csdn.net/NEFU_kadia/article/details/104214133