80分求解

P1303 A*B Problem

zjh202412904238 @ 2024-10-05 17:56:56

#include<stdio.h>
#include<string.h>
char s1[5002]={0},s2[5002]={0};
int a[5002],b[5002],c[5002];
int main()
{
    int la,lb,lc;
    scanf("%s %s",&s1,&s2);
    la = strlen(s1);
    lb = strlen(s2);
    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';
    }
    lc=la+lb;
    for(int i=1;i<=la;i++)
    {
        for(int j=1;j<=lb;j++)
         {
          c[i+j-1] += a[i]*b[j];
          c[i+j] += c[i+j-1]/10;
          c[i+j-1] = c[i+j-1]%10;
         }
    }
    if(c[lc]==0&&lc>0) lc--;
    for(int i=lc;i>0;i--)
    {
        printf("%d",c[i]);
    }
    return 0;
}

by liruizhou_lihui @ 2024-10-05 18:00:31

@zjh202412904238

输入:

114514
  0

你的代码输出:

000000

by wujunxi206 @ 2024-10-05 18:03:52

@zjh202412904238 调了一下(求关)

#include<stdio.h>
#include<string.h>
char s1[5002]={0},s2[5002]={0};
int a[5002],b[5002],c[5002];
int main(){
    int la,lb,lc;
    scanf("%s %s",&s1,&s2);
    la = strlen(s1);
    lb = strlen(s2);
    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';
    }
    lc=la+lb;
    for(int i=1;i<=la;i++){
        for(int j=1;j<=lb;j++){
            c[i+j-1]+=a[i]*b[j];
        }
    }
    int lenc=la+lb-1;
    for(int i=1;i<=lenc;i++){
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    if(c[lenc+1]) lenc++;
    while(c[lenc]==0&&lenc>1){
        lenc--;
    }
    for(int i=lenc;i>=1;i--){
        printf("%d",c[i]);
    }
    return 0;
}

by 0Io_oI0 @ 2024-10-05 18:22:46

@zjh202412904238 求关注

#include<bits/stdc++.h>
using namespace std;
string a,b;
int a1[2005],b1[2005],c[100000];
int main(){
    cin>>a>>b;
    reverse(a.begin(),a.end());
    reverse(b.begin(),b.end());
    for(int i=0;i<a.size();i++)a1[i]=a[i]-'0';
    for(int i=0;i<b.size();i++)b1[i]=b[i]-'0';
    for(int i=0;i<a.size();i++){
        for(int j=0;j<b.size();j++){
            c[i+j]+=a1[i]*b1[j];
        }
    }
    int len=a.size()+b.size();
    for(int i=0;i<len;i++){
        if(c[i]>=10){
            c[i+1]+=c[i]/10;
            c[i]%=10;
        }
    }
    while(len>0&&c[len]==0)len--;
    for(int i=len;i>=0;i--){
        cout<<c[i];
    }
    return 0;
}

by zjh202412904238 @ 2024-10-05 20:06:10

已完成


|