[模板]高精度加、减、乘

1、高精度加法

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int N = 10001;

int main()
{
    char sa[N], sb[N];
    int a[N*2], b[N*2], c[N*2];
    int la, lb, lc;//must lc < la+lb
    int i, j, t;

    scanf("%s%s", sa, sb);
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(c, 0, sizeof(c));
    la = strlen(sa);
    lb = strlen(sb);
    for(i = 0; i < la; i++)
        a[la-i] = sa[i] - '0';
    for(i = 0; i < lb; i++)
        b[lb-i] = sb[i] - '0';
    lc = max(la, lb);
    t = 0;
    for(i = 1; i <= lc; i++)
    {
        c[i] += a[i] + b[i];
        if(c[lc] >= 10)
            lc++;
        t = c[i] / 10;
        c[i] %= 10;
        c[i+1] += t;
    }
    for(i = lc; i > 0; i--)
        printf("%d",c[i]);
    return 0;
}

2、高精度减法

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int N = 10001;

int main()
{
    char sa[N], sb[N], n[N];
    int a[N*2], b[N*2], c[N*2];
    int la, lb, lc;//must lc < la+lb
    int i, j, t;

    scanf("%s%s", sa, sb);
    if(strlen(sa) < strlen(sb) || strlen(sa)==strlen(sb) && strcmp(sa,sb)<0)
    {
        strcpy(n, sa);
        strcpy(sa, sb);
        strcpy(sb, n);
    }
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(c, 0, sizeof(c));
    la = strlen(sa);
    lb = strlen(sb);
    for(i = 0; i < la; i++)
        a[la-i] = sa[i] - '0';
    for(i = 0; i < lb; i++)
        b[lb-i] = sb[i] - '0';
    lc = max(la, lb);
    t = 0;
    for(i = 1; i <= lc; i++)
    {
        if(a[i] < b[i])
        {
            a[i] += 10;
            a[i+1]--;
        }
        c[i] = a[i] - b[i];
    }
    while(c[lc]==0 && lc>1)//notice lc>1 (9-9=0
        lc--;
    for(i = lc; i > 0; i--)
        printf("%d",c[i]);
    return 0;
}

3、高精度乘法(两行非负整数)

模板1:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int N = 10001;

int main()
{
    char sa[N], sb[N];
    int a[N*2], b[N*2], c[N*2];
    int la, lb, lc;//must lc < la+lb
    int i, j, t;

    scanf("%s%s", sa, sb);
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(c, 0, sizeof(c));
    la = strlen(sa);
    lb = strlen(sb);
    for(i = 0; i < la; i++)
        a[la-i] = sa[i] - '0';
    for(i = 0; i < lb; i++)
        b[lb-i] = sb[i] - '0';
    for(i = 1; i <= la; i++)
    {
        t = 0;
        for(j = 1; j <= lb; j++)
        {
            c[i+j-1] += t + a[i] * b[j];
            t = c[i+j-1] / 10;
            c[i+j-1] %= 10;
        }
        c[i+lb] = t;
    }
    lc = la + lb;
    while(c[lc] == 0 && lc)
        lc--;
    for(i = lc; i > 0; i--)
        printf("%d",c[i]);
    return 0;
}


模板2:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int N = 10001;

int main()
{
    char sa[N], sb[N];
    int a[N*2], b[N*2], c[N*2];
    int la, lb, lc;//must lc < la+lb
    int i, j, t;

    scanf("%s%s", sa, sb);
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(c, 0, sizeof(c));
    la = strlen(sa);
    lb = strlen(sb);
    for(i = 0; i < la; i++)
        a[la-i] = sa[i] - '0';
    for(i = 0; i < lb; i++)
        b[lb-i] = sb[i] - '0';
    //用数组模拟运算
    for(i = 1; i <= la; i++)
        for(j = 1, lc = i-1; j <= lb; j++)
            c[++lc] += a[i] * b[j];//notice +=
    //进位处理
    for(i = 1; i <= lc-1; i++)
        if(c[i] >= 10)
        {
            if(c[lc] >= 10)
                lc++;
            t = c[i] / 10;
            c[i] %= 10;
            c[i+1] += t;
        }
    for(i = lc; i > 0; i--)
        printf("%d",c[i]);
    return 0;
}

(4、高精度除法待补)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值