40求助!!!

P1303 A*B Problem

Xubeibei @ 2024-07-24 09:16:06

#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<int> mul(vector<int> &A,int &b){
    vector<int>C;
    int t=0;
    for(int i=0;i<A.size();i++){
        t+=A[i]*b;
        C.push_back(t%10);
        t/=10;
    }
    if(t)C.push_back(t);
    return C;
}
int main(){
    string a;
    vector<int>A;
    int b;
    cin>>a>>b;
    for(int i=a.length()-1;i>=0;i--)A.push_back(a[i]-'0');
    vector<int> C=mul(A,b);
    for(int i=C.size()-1;i>=0;i--)printf("%d",C[i]);
}

by __1943 @ 2024-07-24 17:11:59

你想的有点复杂

#include<bits/stdc++.h>
using namespace std;
const int N =2e3+10;
int a[N],b[N],c[2*N];
string s1,s2;
int main()
{
    cin>>s1>>s2;
    int len1=s1.size(),len2=s2.size();
    for(int i=0;i<len1;i++)
    {
        a[i]=s1[len1-1-i]-48;
    }
    for(int i=0;i<len2;i++)
    {
        b[i]=s2[len2-1-i]-48;
    }
    //模拟乘法
    for(int j=0;j<len2;j++)
    {
        for(int i=0;i<len1;i++)
        {
            c[i+j]+=a[i]*b[j];
        }
    }
    int len =len1+len2;
    for(int i=0;i<len;i++)
    {
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    while(c[len]==0&&len) len--;
    for(len;len>=0;len--) cout<<c[len];
    return 0;
}

|