都开到4020了怎么还RE

P1303 A*B Problem

bluetored @ 2022-10-18 21:23:12

#include<iostream>
#include<cstring>
using namespace std;
int a[2015], b[2005], ans[4020];
void rstring(char s1[], char s2[])
{
    a[0] = strlen(s1);
    b[0] = strlen(s2);
    for (int i = a[0], j = 0; i >= 1; i--, j++)
    {
        a[i] = s1[j] - '0';
    }
    for (int i = b[0], j = 0; i >= 1; i--, j++)
    {
        b[i] = s2[j] - '0';
    }
    return;
}
void multiplication(int a[], int b[])
{
    for (int i = 1; i <= a[0]; i++)
    {
        for (int j = 1; j <= b[0]; j++)
        {
            ans[i + j - 1] = ans[i + j - 1] + a[i] * b[j];
            ans[i + j] += ans[i + j - 1] / 10;
            ans[i + j - 1] = ans[i + j - 1] % 10;
        }
    }
    ans[0] = a[0] + b[0];
    return;
}
void print(int ans[])
{
    int len = ans[0];
    while (ans[len] == 0 && len != 1)
    {
        len--;
    }
    while (len > 0)
    {
        cout << ans[len--];
    }
    return;
}
int main()
{
    char s1[1005], s2[1005];
    cin >> s1 >> s2;
    rstring(s1, s2);
    multiplication(a, b);
    print(ans);
    return 0;
}

by Vanishing_Stars @ 2022-10-18 21:34:39

@bluetored 不要写函数,直接在主程序写,可能在传递参数的过程中出问题了。?


by Untitled10032 @ 2022-10-18 21:57:35

因为您的主函数里的 s1 和 s2 大小还是 1005。


by Z_X_D_ @ 2022-10-18 22:08:41

您写法不对 WA on #5了

RE修改代码:

#include<iostream>
#include<cstring>
using namespace std;
int a[2015], b[2005], ans[10000];
void rstring(char s1[], char s2[])
{
    a[0] = strlen(s1);
    b[0] = strlen(s2);
    for (int i = a[0], j = 0; i >= 1; i--, j++)
    {
        a[i] = s1[j] - '0';
    }
    for (int i = b[0], j = 0; i >= 1; i--, j++)
    {
        b[i] = s2[j] - '0';
    }
    return;
}
void multiplication(int a[], int b[])
{
    for (int i = 1; i <= a[0]; i++)
    {
        for (int j = 1; j <= b[0]; j++)
        {
            ans[i + j - 1] = ans[i + j - 1] + a[i] * b[j];
            ans[i + j] += ans[i + j - 1] / 10;
            ans[i + j - 1] = ans[i + j - 1] % 10;
        }
    }
    ans[0] = a[0] + b[0];
    return;
}
void print(int ans[])
{
    int len = ans[0];
    while (ans[len] == 0 && len != 1)
    {
        len--;
    }
    while (len)
    {
        cout << ans[len--];
    }
    exit(0);
}
int main()
{
    char s1[1005], s2[1005];
    cin >> s1 >> s2;
    rstring(s1, s2);
    multiplication(a, b);
    print(ans);
}

WA修改代码:

#include<iostream>
#include<cstring>
using namespace std;
int a[2015], b[2005], ans[4020];
void rstring(char s1[], char s2[])
{
    a[0] = strlen(s1);
    b[0] = strlen(s2);
    for (int i = a[0], j = 0; i >= 1; i--, j++)
    {
        a[i] = s1[j] - '0';
    }
    for (int i = b[0], j = 0; i >= 1; i--, j++)
    {
        b[i] = s2[j] - '0';
    }
    return;
}
void multiplication(int a[], int b[])
{
    for (int i = 1; i <= a[0]; i++)
    {
        for (int j = 1; j <= b[0]; j++)
        {
            ans[i + j - 1] = ans[i + j - 1] + a[i] * b[j];
            ans[i + j] += ans[i + j - 1] / 10;
            ans[i + j - 1] = ans[i + j - 1] % 10;
        }
    }
    ans[0] = a[0] + b[0];
    return;
}
void print(int ans[])
{
    int len = ans[0];
    while (ans[len] == 0 && len != 1)
    {
        len--;
    }
    while (len > 0)
    {
        cout << ans[len--];
    }
    exit(0);
}
int main()
{
    char s1[2005], s2[2005];
    cin >> s1 >> s2;
    rstring(s1, s2);
    multiplication(a, b);
    print(ans);
}

by bluetored @ 2022-10-19 15:15:43

@Untitled10032 感谢


by bluetored @ 2022-10-19 15:16:02

@Z_XD 感谢感谢


|