这为啥啊,x放里面AC,放外面20

题目总版

TMQQtt @ 2024-11-10 17:34:20

20分代码:
#include<iostream>
#include<cstring>
using namespace std;
const int N=5010;
char al[N],bl[N];
int a[N],b[N],c[N*2];
int main()
{
    cin>>al>>bl;
    int la,lb;
    la=strlen(al),lb=strlen(bl);
    int x=0;
    for(int i=0;i<la;++i) 
    {
        a[la-i]=al[i]-'0';
    }
    for(int i=0;i<lb;++i) 
    {
        b[lb-i]=bl[i]-'0';
    }
    int lc=la+lb;
    for(int i=1;i<=la;++i)
    {
        //int x=0;
        for(int j=1;j<=lb;++j)
        {
            c[i+j-1]+=a[i]*b[j]+x;
            x=c[i+j-1]/10;
            c[i+j-1]=c[i+j-1]%10;
        }
        c[i+lb]+=x;
    }
    while(c[lc]==0&&lc>1) lc--;
    for(int i=lc;i>=1;--i) cout<<c[i];
    return 0;
}
AC代码
#include<iostream>
#include<cstring>
using namespace std;
const int N=5010;
char al[N],bl[N];
int a[N],b[N],c[N*2];
int main()
{
    cin>>al>>bl;
    int la,lb;
    la=strlen(al),lb=strlen(bl);
    for(int i=0;i<la;++i) 
    {
        a[la-i]=al[i]-'0';
    }
    for(int i=0;i<lb;++i) 
    {
        b[lb-i]=bl[i]-'0';
    }
    int lc=la+lb;
    for(int i=1;i<=la;++i)
    {
        int x=0;
        for(int j=1;j<=lb;++j)
        {
            c[i+j-1]+=a[i]*b[j]+x;
            x=c[i+j-1]/10;
            c[i+j-1]=c[i+j-1]%10;
        }
        c[i+lb]+=x;
    }
    while(c[lc]==0&&lc>1) lc--;
    for(int i=lc;i>=1;--i) cout<<c[i];
    return 0;
}

by Remarks @ 2024-11-10 17:38:26

@TMQQtt 放外面每次循环开始的时候 x 不归零。


by _luogu_huowenshuo_ @ 2024-11-10 17:38:46

@TMQQtt 哪道题???


by codingwen @ 2024-11-10 17:38:58

@TMQQtt 初始化


by TMQQtt @ 2024-11-10 17:46:11

@_luoguhuowenshuo 高精乘模板


by TMQQtt @ 2024-11-10 17:46:32

@Remarks 明白了,谢谢佬


|