#4#5RE

P1303 A*B Problem

tangyiqi @ 2024-08-25 22:09:39

#include <bits/stdc++.h>
using namespace std;
string s1,s2;
const int maxn = 300;
int a[maxn],b[maxn],c[maxn];
int main(){
    cin>>s1>>s2;
    int la = s1.size(),lb = s2.size();
    int lc = la+lb;
    for(int i = 0;i<la;i++){
        a[la-i] = s1[i]-'0';
    }
    for(int i = 0;i<lb;i++){
        b[lb-i] = s2[i]-'0';
    }
    for(int i = 1;i<=la;i++){
        for(int j = 1;j<=lb;j++){
            c[i+j-1]+=a[i]*b[j];
        }
    }
    for(int i = 1;i<lc;i++){
        c[i+1]+=c[i]/10;
        c[i] = c[i]%10;
    }
    if(c[lc+1]!=0) lc++;
    while(c[lc]==0&&lc>1) lc--;
    for(int i = lc;i>0;i--){
        cout<<c[i];
    }
    return 0;
};

by Innate_Joker @ 2024-08-25 22:16:30

@tangyiqi 数组开小了,建议先看看题面。


by Innate_Joker @ 2024-08-25 22:16:53

#include<cstring>
#include<iostream>
using namespace std;
char str[2001];
int a[2001],b[2001],c[400100];
int main()
{
    cin>>str;
    int lena=strlen(str);
    for(int i=1; i<=lena; i++)
    {
        a[i]=str[lena-i]-48;
    }
    cin>>str;
    int lenb=strlen(str);
    for(int i=1; i<=lenb; i++)
    {
        b[i]=str[lenb-i]-48;
    }
    int x;
    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;
    }
    int lenc=lena+lenb;
    while(c[lenc]==0&&lenc>1)
    {
        lenc--;
    }
    for(int i=lenc; i>=1; i--)
    {
        cout<<c[i];
    }
    return  0;
}

by Innate_Joker @ 2024-08-25 22:17:24

求关 @tangyiqi


by Innate_Joker @ 2024-08-25 22:18:15

加一句,貌似你已经A了。


by tangyiqi @ 2024-08-26 18:41:04

@Innate_Joker
已关,谢谢


|