【算法】高精度

高精度加法

步骤

1.将两个加数以字符串形式输入
2.分别将两数倒序存入数组中
3.对应位分别相加
4.处理进位
5.输出结果

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

string x, y;
int la, lb, lc;
int a[N], b[N], c[N];

void add(int c[],int a[],int b[])
{
    for (int i = 0; i < lc;i++)
    {
        c[i] += a[i] + b[i];
        c[i + 1] += c[i] / 10;
        c[i] %= 10;
    }
    if(c[lc])
        lc++;
    for (int i = lc-1; i >=0;i--)
    {
        cout << c[i];
    }
    cout << endl;
}
int main()
{
    cin >> x >> y;
    la = x.size(), lb = y.size();
    lc = max(la, lb);
    for (int i = 0; i < la;i++)
        a[i] = x[la - i - 1] - '0';

    for (int i = 0; i < lb;i++)
        b[i] = y[lb - i - 1] - '0';

    add(c, a, b);

    return 0;
}

高精度减法

步骤

  1. 将被减数与减数以字符串形式输出
  2. 判断两数长度 计算原理为用长的数减短的数
  3. 分别将两数倒序存放在数组中
  4. 逐位相减 处理借位
  5. 处理前导零
  6. 输出结果
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

string x, y;
int la, lb, lc;
int a[N], b[N], c[N];

bool cmp(string& x,string& y)
{
    if(x.size() != y.size())
        return x.size() < y.size();
    return x < y;
}

void sub(int c[],int a[],int b[])
{
    for (int i = 0; i < lc;i++)
    {
        c[i] += a[i] - b[i];
        if(c[i] < 0)
        {
            c[i] += 10;
            c[i + 1] -= 1;
        }
    }

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

    for (int i = lc - 1; i >= 0;i--)
    {
        cout << c[i];
    }
    cout << endl;
}

int main()
{
    cin >> x >> y;
    if(cmp(x,y))
    {
        cout << '-';
        swap(x, y);
    }
    la = x.size(), lb = y.size(), lc = max(la, lb);
    for (int i = 0; i < la;i++)
        a[i] = x[la - i - 1] - '0';

    for (int i = 0; i < lb;i++)
        b[i] = y[lb - i - 1] - '0';

    sub(c, a, b);

    return 0;
}

高精度乘法

步骤

  1. 将两个乘数分别以字符串的形式输入
  2. 分别倒序存储在数组中
  3. 逐位计算乘法(参考小学乘法竖式)
  4. 处理进位、前导零
  5. 输出结果
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

string x, y;
int la, lb, lc;
int a[N], b[N], c[N];


void mul(int c[],int a[],int b[])
{
    for (int i = 0; i < la;i++)
    {
        for (int j = 0; j < lb;j++)
        {
            c[i + j] += a[i] * b[j];
        }
    }

    //处理进位
    for (int i = 0; i < lc;i++)
    {
        c[i + 1] += c[i] / 10;
        c[i] %= 10;
        
    }

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

    for (int i = lc - 1; i >= 0;i--)
    {
        cout << c[i];
    }
    cout << endl;
}

int main()
{
    cin >> x >> y;
    
    la = x.size(), lb = y.size(), lc = la + lb;
    for (int i = 0; i < la;i++)
        a[i] = x[la - i - 1] - '0';

    for (int i = 0; i < lb;i++)
        b[i] = y[lb - i - 1] - '0';

    mul(c, a, b);
    return 0;
}

高精度乘法

步骤

  1. 将被除数(高精度)以字符串形式输入,除数以整型输入
  2. 将被除数倒序存入数组中
  3. 从高位到低位逐位计算(借鉴小学除法竖式:计算当前位的被除数 并记录本次余数)
  4. 处理前导零
  5. 输出结果
#include <bits/stdc++.h>

using namespace std;
const int N = 1e6 + 10;

string x;
int b;
int a[N], c[N];
int la, lc;

void sub(int c[],int a[], int b)
{
    long long t = 0;
    for (int i = la - 1; i >= 0;i--)
    {
        t = t * 10 + a[i];
        c[i] = t / b;
        t %= b;
    }

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

    for (int i = lc - 1; i >= 0;i--)
        cout << c[i];
    cout << endl;
}
int main()
{
    cin >> x >> b;
    la = lc = x.size();
    for (int i = 0; i < la;i++)
    {
        a[i] = x[la - i - 1] - '0';
    }

    sub(c, a, b);

    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值