zoukankan      html  css  js  c++  java
  • 模拟与高精度 P1303 A*B Problem

    题目

    https://www.luogu.com.cn/problem/P1303

    代码

    #include<iostream>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #include<cstdio>
    #include<vector>
    using namespace std;
    #define max 2010
    vector<int>p;
    void mul(string a, string b)
    {
    
        int t = 0;
        for (int i = 0; i < a.length(); i++)
        {
            for (int j = 0; j < b.length(); j++)
            {
                p[i+j]+=((a[i] - '0')* (b[j] - '0'));//注意是加! 注意这里的p[i+j]!  先乘起来,后面再统一进位
            }
        }
        for (int i = 0; i <max*2; i++)//max * 2是保证遍历完进位前p【】有数字的地方
        {
            if (p[i] >=10)
            {
                p[i + 1] += p[i] / 10;//可能最高位还要进位,所以p预先resize的大小为max*2+2,保证进位有空间记录
                p[i] = p[i] % 10;
            }
        }
    }
    int main()
    {
        std::ios::sync_with_stdio(false);
        std::cin.tie(0);
        std::cout.tie(0);
        string a, b;
        cin >>a >> b;
        reverse(a.begin(), a.end());
        reverse(b.begin(), b.end());
        p.resize(max*2+2);
        mul(a, b);
        bool ok = false;
        for (int i = p.size() - 1; i >= 0; i--)
        {
            if (p[i] != 0)ok = true;
            if (ok)cout << p[i];
        }
        if (!ok)cout << 0;
    }
     
  • 相关阅读:
    sqhhb
    12333
    12

    今日份
    12
    彻底理解 Cookie、Session、Token
    https原理
    12312
    uiower
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12834871.html
Copyright © 2011-2022 走看看