求调整qwq

P1303 A*B Problem

wisdom2010 @ 2024-02-17 16:20:22

#include<bits/stdc++.h>
using namespace std;
string m, n;
const int maxn = 2000 + 5;
int a[maxn] = {0}, b[maxn] = {0};
int c[maxn][maxn] = {0};
int sum[maxn] = {0};
int cnt = 0;
int main()
{
    cin >> m >> n;
    int len = max(m.size(), n.size()) - 1;
    int ans = min(m.size(), n.size()) - 1;
    for(int i = 0; i < m.size(); i++)
        a[i] = m[m.size() - 1 - i] - '0';
    for(int i = 0; i < n.size(); i++)
        b[i] = n[n.size() - 1 - i] - '0';
    for(int i = 0; i < ans; i++)
    {
        for(int j = 0; j < len; j++)
        {
            cnt++;
            c[i][i + j] += b[i] * a[j];
            if(c[i][i + j] >= 10)
            {
                c[i][i + j + 1] += c[i][j] / 10;
                c[i][i + j] /= 10;
            }
        }
    }
    for(int i = 0; i < cnt; i++)
    {
        for(int j = i; j <= ans + 1; j++)
        {
            sum[i] += c[j][i];
            if(sum[i] >= 10)
            {
                sum[i + 1] += sum[i] / 10;
                sum[i] %= 10;
            }
        }
    }
    for(int i = cnt - 1; i > 0; i++)
    {
        cout << sum[i];
    }
    return 0;
}

by laozhang_123 @ 2024-02-17 16:23:57

#include<bits/stdc++.h>
using namespace std;
int main(){
    string a,b;
    int c[2010],d[2010],e[4010];
    memset(e,0,sizeof(e));
    cin>>a>>b;
    if(a=="0"||b=="0"){
        cout<<0;
        return 0;}
    int lena=a.length(),lenb=b.length();
    for(int i=1,j=lena-1;j>=0;i++,j--) c[i]=a[j]-'0';
    for(int i=1,j=lenb-1;j>=0;i++,j--) d[i]=b[j]-'0';
    for(int i=1;i<=lena;i++){
        for(int j=1;j<=lenb;j++)e[i+j-1]+=c[i]*d[j];}
    int len=lena+lenb;
    for(int i=1;i<=len;i++){
        e[i+1]+=e[i]/10;
        e[i]%=10;}
    while(!e[len]) len--;
    for(int i=len;i>=1;i--) cout<<e[i];
    return 0;
}

|