20分跪求

P1303 A*B Problem

The_fisher @ 2024-01-29 11:45:21

1ac 2,3都wa 4,5更离谱 直接re

#include<bits/stdc++.h>
using namespace std;
int main()
{
    char a1[100],b1[100];
    int a[100],b[100],c[100],lena,lenb,lenc,i,j,x;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    scanf("%s%s",a1,b1);
    lena=strlen(a1);lenb=strlen(b1);
    for (i=0;i<=lena-1;i++) a[lena-i]=a1[i]-48;
    for (i=0;i<=lenb-1;i++) b[lenb-i]=b1[i]-48;
    for (i=1;i<=lena;i++)
    {
        x=0;     
        for (j=1;j<=lenb;j++)              
        {
            c[i+j-1]=a[i]*b[j]+x+c[i+j-1]; 
            c[i+j-1] %= 10;
        }
        c[i+lenb]=x;                                
    }
    lenc=lena+lenb;
    while (c[lenc]==0&&lenc>1)    
        lenc--;
    for (i=lenc;i>=1;i--)
        cout<<c[i];
    cout<<endl;
    return 0;
}

by The_fisher @ 2024-01-29 11:46:01

跪求大佬


by Special_Tony @ 2024-01-29 11:57:52

@zhaoyuhang1231 我问你 (10^{101}-1)\times(10^{101}-1) 有几位


by Special_Tony @ 2024-01-29 11:58:29

@zhaoyuhang1231 另外你不看数据范围吗,2000的范围你只开100,不RE才怪


by 2023tanglehao @ 2024-01-29 12:05:41

@zhaoyuhang1231 代码帮你改好了

#include<bits/stdc++.h>
using namespace std;
char a1[50001],b1[50001];
int a[50001],b[50001],c[50001],lena,lenb,lenc,i,j;//数组开大一点就不会RE了
  //放在外面就不用初始化啦~
int main()
{

    scanf("%s%s",a1,b1);
    lena=strlen(a1);lenb=strlen(b1);
    for (i=1;i<=lena;i++) a[i]=a1[lena-i]-'0';
    for (i=1;i<=lenb;i++) b[i]=b1[lenb-i]-'0';
  //是这样写哦
    for (i=1;i<=lena;i++)
    {   
        for (j=1;j<=lenb;j++)              
        {
            c[i+j-1]+=a[i]*b[j]; //要注意是+=
        }                            
    }
    for(i=1;i<lena+lenb;i++)
        if(c[i]>9)
        {
            c[i+1]+=c[i]/10;
            c[i]%=10;
        }
  //处理进位在外面会方便一点
    lenc=lena+lenb;
    while (c[lenc]==0&&lenc>1)    
        lenc--;
    for (i=lenc;i>=1;i--)
        cout<<c[i];
    cout<<endl;
    return 0;
}

by The_fisher @ 2024-01-29 13:54:36

谢谢大佬们 关注瞻仰


|