A+B & A*B 问题

大家好,我是Zac,最近正在疯狂写文章(为了上分~😁上大师👑)今天我又来发文章了(给点儿面子,给个赞吧🙏🏼)  ↓

题目描述

高精度加法和乘法,不用考虑负数

输入格式

分两行输入a,b≤10^2000。

输出格式

输出两行,代表a+b、a*b 的值。

输入输出样例

1
1
2
1

话不多说,上代码:

AC代码:

#include <iostream>
#include <string>
#include <cmath>
#define maxn 5010
int a[maxn], b[maxn], c[maxn];
int a2[maxn], b2[maxn], c2[maxn];
using namespace std;
int main() {
    string A, B;
    cin >> A >> B;
    int len = max(A.size(), B.size());
    for (int i = A.size() - 1, j = 1; i >= 0; i--, j++) {
        a[j] = A[i] - '0';
    }
    for (int i = B.size() - 1, j = 1; i >= 0; i--, j++) {
        b[j] = B[i] - '0';
    }
    for (int i = 1; i <= len; i++) {
        c[i] += a[i] + b[i];
        c[i + 1] = c[i] / 10;
        c[i] %= 10;
    }
    if (c[len + 1]) {
        len++;
    }
    for (int i = len; i >= 1; i--) {
        cout << c[i];
    }
    cout << endl;
    //----------*----------
    if (A == "0" or B == "0") {
        cout << 0 << endl;
        return 0;
    }
    int len1 = A.size(), len2 = B.size();
    for (int i = len1 - 1, j = 1; i >= 0; i--, j++) {
        a2[j] = A[i] - '0';
    }
    for (int i = len2 - 1, j = 1; i >= 0; i--, j++) {
        b2[j] = B[i] - '0';
    }
    for (int i = 1; i <= len1; i++) {
        for (int j = 1; j <= len2; j++) {
            c2[i + j - 1] += a2[i] * b2[j];
        }
    }
    len = len1 + len2;
    for (int i = 1; i <= len; i++) {
        c2[i + 1] += c2[i] / 10;
        c2[i] %= 10;
    }
    for ( ; !c[len]; ) {
        len--;
    }
    for (int i = len; i >= 1; i--) {
        cout << c2[i];
    }
    return 0;
}

endline of the paragraph: 

👍🏼✅📂🙏🏼👋🏼

```end……

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值