高精度乘- A*B Problem

# A*B Problem

注意点:要考虑字符串输入为0的情况,数组要开大一点

## 题目背景

高精度乘法模板题。

## 题目描述

给出两个非负整数,求它们的乘积。

## 输入格式

输入共两行,每行一个非负整数。

## 输出格式

输出一个非负整数表示乘积。

## 样例 #1

### 样例输入 #1

```

2
```

### 样例输出 #1

```
2
```

## 提示

每个非负整数不超过 $10^{2000}$。

#include <iostream>
using namespace std;
#include <string.h>
#include <math.h>

int c[100001];
int a[10000], b[10000];
void strReserve(string s, int d[])
{
    for (int i = 0; i < s.size(); i++)
    {
        d[s.size() - i] = s[i] - '0';
    }
}
int main()
{
    string s1, s2;
    
    cin >> s1 >> s2;
    if (s1 == "0" || s2 == "0")
    {
        cout << 0;
        return 0;
    }
    strReserve(s1, a);
    strReserve(s2, b);
    
    int la = s1.size();
    int lb = s2.size();
    int lc = la + lb;

    for (int i = 1; i <= la; i++)
    {
        for (int j = 1; j <= lb; j++)
        {
            c[i + j - 1] += a[i] * b[j];
            c[i + j] += c[i + j - 1] / 10;
            c[i + j - 1] %= 10;
        }
    }
    while (c[lc] == 0 && lc >= 1)lc--;
    for (int i = lc; i>0; i--)
    {
        cout << c[i];
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值