蓝桥杯算法训练-大数字加减乘除

  • 加法和乘法
#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e4 + 5;           // 时间复杂度是O(n^2),最多到1e4
int ans[maxn];                       // 保存答案
int cnt;                             // 数组长度

void multy(string a, string b) {
    cnt = 0;
    memset(ans, 0, sizeof ans);
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    int n = a.size(), m = b.size();
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            ans[i + j] += (a[i] - '0') * (b[j] - '0');
        }
    }
    for (int i = 0; i < m + n; i++) {
        ans[i + 1] += ans[i] / 10;
        ans[i] %= 10;
    }
    cnt = m + n;
    while (ans[cnt] == 0) cnt--;
    cnt++;
    reverse(ans, ans + cnt);
}

void add(string a, string b) {
    memset(ans, 0, sizeof ans);
    cnt = 0;
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    int n = a.size(), m = b.size(), t = 0;
    for (int i = 0, j = 0; i < n || j < m; i++, j++, cnt++) {
        if (i < n) t += a[i] - '0';
        if (j < m) t += b[j] - '0';
        ans[cnt] = t % 10;
        t /= 10;
    }
    if (t) ans[cnt++] = t;
    reverse(ans, ans + cnt);
}

int main() {

    string a, b;
    cin >> a >> b;
    add(a, b);
    for (int i = 0; i < cnt;i ++) {
        printf("%d", ans[i]);
    }
    return 0;
}

  • 除法,适用于被除数是整形的情况。
#include <bits/stdc++.h>

using namespace std;



vector<int>div(vector<int>&a, int b) {
    int m = a.size();
    vector<int>c;

    int j = 0;
    int mod = 0;
    int bg = 1;

    while(j < m) {
        int x = mod;
        while(j < m && x < b) {
            x = x * 10 + a[j];
            if(x < b && !bg) c.push_back(0);
            j++;
        }
        bg = 0;

        if(x >= b) c.push_back(x / b);
        //printf("x = %d push_%d mod = %d\n", x, x / b, x % b);
        mod = x % b;
    }
    c.push_back(mod);                   //最后一个存放mod;
    return c;
}


vector<int> change(string &s) {
    vector<int>ans;
    int n = s.size();
    for(int i = 0; i < n; i++) 
    {
        ans.push_back(s[i] - '0');
    }
    return ans;
}


int main() {
    string s1;
    int b;
    getline(cin, s1);
    cin >> b;

    vector<int>a = change(s1);

    vector<int>c = div(a, b);

    for(int i = 0; i < c.size() - 1; i++) printf("%d", c[i]);
    if(c.size() == 1) printf("0");
    printf("\n%d", c[c.size() - 1]);
}

作者:Empty_94
链接:https://www.acwing.com/activity/content/code/content/1840553/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 减法
#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

vector<int> sub(vector<int> a, vector<int> b)
{
    vector<int> res;
    int t = 0;
    for (int i = 0; i < a.size(); i++)
    {
        int x = a[i] + t;
        if (i < b.size())
            x -= b[i];
        if (x >= 0)
        {
            res.push_back(x);
            t = 0;
        }
        else{
            res.push_back(x + 10);
            t = -1;
        }
    }
    while (res.size() > 1 && res.back() == 0) res.pop_back();
    return res;
}

bool judge(string &a, string &b) {
    if(a.size() > b.size()) return true;
    else if(a.size() < b.size()) return false;
    else return a > b;
}

int main()
{
    string n, m;
    cin >> n >> m;
    vector<int> a, b;
    bool flag = true;
    if (judge(m, n))
    {
        flag = false;
        string t = m;
        m = n;
        n = t;
    }
    for (int i = n.size() - 1; i >= 0; i--)
        a.push_back(n[i] - '0');
    for (int i = m.size() - 1; i >= 0; i--)
        b.push_back(m[i] - '0');
    auto res = sub(a, b);
    if (!flag)
        cout << '-';
    for (int i = res.size() - 1; i >= 0; i--)
            printf("%d", res[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值