0分求调(有答必关)

P1303 A*B Problem

TripFriend1130 @ 2025-01-04 15:56:56

自己测试过(没用样例),1*2是可以算的,但交上去就是听取WA声一片。。。\ 代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
class BigNum{
    public:
    int n;
    int d[4000005];
    BigNum()
    {
        n = 0;
        memset(d,0,sizeof(d));
    }
    void read()
    {
        char s[2005];
        scanf("%s",s);
        n = strlen(s);
        for(int i = 0;i<n;i++)
        {
            d[i] = s[n-i-1]-'0';
        }
    }
    BigNum operator*(const BigNum &b)
    {
        BigNum c;
        for(int i = 0;i<n;i++)
        {
            int num = i;
            for(int j = 0;j<b.n;j++)
            {
                c.d[i+j] += d[i]*b.d[j];
                if(c.d[i+j]>9)
                {
                    c.d[i+j+1] = c.d[i+j]/10;
                    c.d[i+j]%=10;
                }
                num++;
            }
            if(c.d[b.n+i+1])
            {
                num++;
            }
            c.n += num-c.n; 
        }
        return c;
    }
};
int main()
{
    BigNum a,b;
    a.read();
    b.read();
    BigNum c = a*b;
    for(int i = c.n-1;i>=0;i--)
    {
        cout << c.d[i];
    }
    return 0;
}

by Endless_Freedom @ 2025-01-04 17:06:09

@TripFriend1130 AI代码能对?


by TripFriend1130 @ 2025-01-04 17:08:54

@Endless_Freedom真不是AI写的,上课跟着老师敲得


by supermzc @ 2025-01-04 17:36:30

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
class BigNum{
    public:
    int n;
    int d[4000005];
    BigNum()
    {
        n = 0;
        memset(d,0,sizeof(d));
    }
    void read()
    {
        char s[2005];
        scanf("%s",s);
        n = strlen(s);
        for(int i = 0;i<n;i++)
        {
            d[i] = s[n-i-1]-'0';
        }
    }
    BigNum operator*(const BigNum &b)
    {
        BigNum c;
        for(int i = 0;i<n;i++)
        {
            int num = i;
            for(int j = 0;j<b.n;j++)
            {
                c.d[i+j] += d[i]*b.d[j];
                if(c.d[i+j]>9)
                {
                    c.d[i+j+1] = c.d[i+j]/10;
                    c.d[i+j]%=10;
                }
                num++;
            }
            if(c.d[b.n+i+1])
            {
                num++;
            }
            c.n += num-c.n; //这里等效c.n=num,但是num是下标,c.n是长度但num是下标
        }
        return c;
    }
};
int main()
{
    BigNum a,b;
    a.read();
    b.read();
    BigNum c = a*b;
    for(int i = c.n-1;i>=0;i--)
    {
        cout << c.d[i];
    }
    return 0;
}

@TripFriend1130


|