参考书上的代码,结果只有20

P1303 A*B Problem

yinbe @ 2023-06-27 14:52:27

#include<iostream>
using namespace std;
string a1,b1;
int a[2005],b[2005],c[5000000],lena,lenb,lenc,x;
int main()
{
    cin>>a1;
    cin>>b1;
    lena=a1.size();
    lenb=b1.size();
    for(int i=0;i<lena;i++)a[lena-i]=a1[i]-'0';
    for(int i=0;i<lenb;i++)b[lenb-i]=b1[i]-'0';
    for(int i=1;i<=lena;i++)
    {
        x=0;
        for(int j=1;j<=lenb;j++)
        {
            c[i+j-1]=a[i]*b[j]+x+c[i+j-1];
            x=c[i+j-1]/10;
            c[i+j-1]%=10;
        }
        c[i+lenb]=x;
    }
    lenc=lena+lenb;
    while(a[lenc]==0&&lenc>1)lenc--;
    for(int i=lenc;i>=1;i--)
    {
        printf("%d",c[i]);
    }
    return 0;
}

by codejiahui @ 2023-06-27 15:06:29

@LuYe0219

高精封装


by _buzhidao_ @ 2023-06-27 15:16:47

高精度


by allen2010 @ 2023-06-27 15:46:35

25行,是c不是a


by allen2010 @ 2023-06-27 15:46:52

@LuYe0219


by C_ccx_N @ 2023-06-27 15:48:46

#include<bits/stdc++.h>
using namespace std;
const int N = 4010;
int c[N], d[N], e[N];
int main()
{
    string a,b;
    cin>>a>>b;
    for(int i=0;i<a.size();i++)
        c[a.size() - i]=a[i]-48;
    for(int i=0;i<b.size();i++)
        d[b.size() - i]=b[i]-48;
    for(int i=1;i<=a.size();i++)
    {
        for(int j=1;j<=b.size();j++)
        {
            e[i + j - 1] += c[i] * d[j];
        }
    }
    for(int i=1;i<a.size()+b.size();i++)
    {
        if(e[i]>9)
        {
            e[i+1] += e[i] / 10;
            e[i]%=10;
        }
    }
    int len = a.size() + b.size();
    while (!e[len] && len > 1) -- len;
    for(int i=len;i>=1;i--)
    {
        cout<<e[i];
    }
}

by yinbe @ 2023-06-28 13:44:41

感谢 @C_ccx_N @allen2010 @buzhidao @codejiahui


by yinbe @ 2023-06-28 13:45:19

此贴完结


|