悬2关高精乘法板子红题求助

P1303 A*B Problem

zhangmingsheng3521 @ 2023-08-24 22:14:00

rt.代码只要碰到要进位的情况就会炸.

代码如下(AC给关,但是大框架不能改,有注释,相对友好)

#include"bits/stdc++.h" 
using namespace std;
int a[2002],b[2002],c[4004];//a,b乘数c积
int main()
{
    int mark;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    string s;
    cin>>s;//读入
    int n1=s.size();
    for (int i=0;i<n1;i++)
    {
        a[i]=int(s[i])-48;
    }
    cin>>s; 
    int n2=s.size();
    for (int i=0;i<n2;i++)
    {
        b[i]=int(s[i])-48;
    }
    for (int i=0;i<n1;i++)
    {
        for (int j=0;j<n2;j++)
        {
            c[i+j]+=a[i]*b[j];//乘法
            c[i+j+1]+=c[i+j]/10;//进位
            c[i+j]=c[i+j]%10;
        }
    }
    for (int i=4003;i>=1;i--)//定位首个非0
    {
        if (c[i]!=0)
        {
            mark=i;
            break;
        }
    }
    for (int i=0;i<=mark;i++)//输出
    {
        printf("%d",c[i]);
    } 
    return 0;
}

by Infinity_Fantasy @ 2023-08-24 22:25:43

@gfgoodluck 哇,大佬一看就很会打florr,能不能教我打


by gfgoodluck @ 2023-08-24 22:26:35

@AK_CCF ?草但是c确实开400就够了啊


by gfgoodluck @ 2023-08-24 22:26:46

@gfgoodluck *4000


by Infinity_Fantasy @ 2023-08-24 22:29:34

哦,是的刚刚我胡了一下确实是4000,但是我开了4e6 :-)


by zhangmingsheng3521 @ 2023-08-24 22:30:51

@gfgoodluck 实验证明只要不进位就是对的,所以它是20分


by gfgoodluck @ 2023-08-24 22:35:10

@zhangmingsheng3521


#include"bits/stdc++.h" 
using namespace std;
int a[2002],b[2002],c[4004];//a,b乘数c积
int main()
{
    int mark;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    string s;
    cin>>s;//读入
    int n1=s.size();
    for (int i=0;i<n1;i++)
    {
        a[n1-i-1]=int(s[i])-48;
    }
    cin>>s; 
    int n2=s.size();
    for (int i=0;i<n2;i++)
    {
        b[n2-i-1]=int(s[i])-48;
    }
    for (int i=0;i<n1;i++)
    {
        for (int j=0;j<n2;j++)
        {
            c[i+j]+=a[i]*b[j];//乘法
            c[i+j+1]+=c[i+j]/10;//进位
            c[i+j]=c[i+j]%10;
        }
    }
    for (int i=4003;i>=1;i--)//定位首个非0
    {
        if (c[i]!=0)
        {
            mark=i;
            break;
        }
    }
    for (int i=mark;i>=0;i--)//输出
    {
        printf("%d",c[i]);
    } 
    return 0;
}

by gfgoodluck @ 2023-08-24 22:35:30

@gfgoodluck 已过


by zhangmingsheng3521 @ 2023-08-24 22:36:31

@gfgoodluck 这...我的程序输在哪里...


by gfgoodluck @ 2023-08-24 22:36:57

@zhangmingsheng3521 就是把数倒过来再倒回去就好了(


by zhangmingsheng3521 @ 2023-08-24 22:37:33

@gfgoodluck 。。。。怪不得调了半天没找到问题


上一页 | 下一页