跳到正文

个人记录

高精度除法(高&高)

Owenzjg约 4 分钟
原文

这是 2025年11月10日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2022年10月26日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 4 分钟

版本

  1. v2当前
  2. v1

这个博客已经停止更新,可以到我的个人博客来瞅瞅。

C++
#include<bits/stdc++.h>   
using namespace std;  
int a[500],b[500],c[500];  
int compare(int a[],int b[])
{  
    int i;  
    if(a[0]>b[0])  
        return 1;  
    if(a[0]<b[0])  
        return -1;  
    for(i=a[0];i>0;i--) 
    {  
        if(a[i]>b[i])  
            return 1;  
        if(a[i]<b[i])  
            return -1;  
    }  
    return 0;  
}  
  
void subduction(int a[],int b[]) 
{  
    int flag;  
    int i;  
  
    flag=compare(a,b);  
    if(flag==0)//ÏàµÈ  
    {  
        a[0]=0;  
        return;  
    }  
    if(flag==1)//´óÓÚ  
    {  
        for(i=1;i<=a[0];i++)  
        {  
            if(a[i]<b[i])
            {  
                a[i+1]--;  
                a[i]+=10;  
            }  
            a[i]-=b[i];  
        }  
        while(a[0]>0&&a[a[0]]==0)
            a[0]--;  
        return;  
    }  
}  
int main()  
{  
    char str1[500],str2[500];  
    int i,j;  
  
    memset(a,0,sizeof(a));  
    memset(b,0,sizeof(b));  
    memset(c,0,sizeof(c));  
  
    cin>>str1>>str2;  
    a[0]=strlen(str1);  
    b[0]=strlen(str2);  
    for(i=1;i<=a[0];i++)  
        a[i]=str1[a[0]-i]-'0';  
    for(i=1;i<=b[0];i++)  
        b[i]=str2[b[0]-i]-'0';  
  
  
    int temp[500];  
    c[0]=a[0]-b[0]+1;  
    for(i=c[0];i>0;i--)  
    {  
        memset(temp,0,sizeof(temp));  
  
        for(j=1;j<=b[0];j++)
            temp[j+i-1]=b[j];  
        temp[0]=b[0]+i-1;  
  
        while(compare(a,temp)>=0)
        {  
            c[i]++;  
            subduction(a,temp);  
        }  
    }  
  
    while(c[0]>0&&c[c[0]]==0)  
        c[0]--;  
  
    if(c[0]==0)//Êä³ö½á¹û  
        cout<<0<<endl;  
    else  
    {  
        for(i=c[0];i>0;i--)  
            cout<<c[i];  
        cout<<endl;  
    }  
  
    if(a[0]==0)
        cout<<0<<endl;  
    else  
    {  
        for(i=a[0];i>0;i--)  
            cout<<a[i];  
        cout<<endl;  
    }  
  
    return 0;  
}