自己总结的高精度模板


前言

网上高精度模板太复杂了,自己花了点时间总结了一套高精度模板。都是倒着存的,所以都要倒着输出。

加法

void gadd(int a[],int b[],int c[])
{
    int t=0;
    for(int i=1;i<=max(a[0],b[0]) || t;i++)
    {
        if(i<=a[0]) t+=a[i];
        if(i<=b[0]) t+=b[i];
        c[i]=t%10;
        t/=10;
        c[0]++;
    }
}

减法

bool cmp(int a[],int b[]) //a是否大于等于b
{
    if(a[0]!=b[0]) return a[0] > b[0];
    for(int i=a[0];i>0;i--) if(a[i]!=b[i]) return a[i]>b[i];
    return true;
}
void gsub(int a[],int b[],int c[])
{
    int t=0; //表示借位
    for(int i=1;i<=a[0];i++)
    {
        t=a[i]-t;
        if(i<=b[0]) t-=b[i];
        c[i]=(t+10)%10;
        c[0]++;
        if(t<0) t=1;
        else t=0;
    }
    while(c[c[0]]==0 && c[0]>1) c[0]--;
}

乘法

void gmul(int a[],int b,int c[])
{
     int t=0;
     for(int i=1;i<=a[0] || t;i++)
     {
        if(i<=a[0]) t+=a[i]*b;
        c[0]++;
        c[i]=t%10;
        t/=10;
     }
     while (c[c[0]]==0 && c[0]>1) c[0]--;
}

void gmul(int a[],int b[],int c[])
{
    for (int i=1;i<=a[0];i++)
        for (int j=1;j<=b[0];j++)
            c[i+j-1]+=a[i]*b[j];

    c[0]=a[0]+b[0];

    for (int i=1;i<c[0];++i)
        if (c[i]>9)
        {
            c[i+1]+=c[i]/10;
            c[i]%=10;
        }

    while (c[c[0]]==0 && c[0]>1) c[0]--;

}

除法

void gdiv(int a[],int b,int c[],int &r) 
{
    r=0;
    for(int i=a[0];i>0;i--)
    {
        r=r*10+a[i];
        c[i]=r/b;
        r%=b;
        c[0]++;
    }
    while(c[c[0]]==0 && c[0]>1) c[0]--;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值