【求助】手动输入数据,测试都正常,但WA了4,5两个点

P1303 A*B Problem

jjb_coder @ 2020-08-10 18:19:57

#include<iostream>
#include<string>
using namespace std;
//思路:和普通竖式乘法一样
int main(void)
{
    string a, b;
    int i, j, k;
    int number1[2005], number2[2005], result[4010] = { 0 };
    int center[2005];
    int jing = 0, t, m = 0, logo = 0, l = 0;//m记录从第几位开始加起,logo判别是否超过所乘位数
    cin >> a >> b;
    for (i = 0; i < a.length(); i++)
        number1[i] = a[i] - '0';
    for (i = 0; i < b.length(); i++)
        number2[i] = b[i] - '0';
    for (i = b.length() - 1; i >= 0; i--)
    {
        logo = 0;
        for (j = a.length() - 1; j >= 0; j--)
        {
            t = number1[j] * number2[i];
            if (t / 10)
            {
                center[a.length() - 1 - j] = t % 10 + jing;//center逆序保存中间结果
                jing = t / 10;
            }
            else
            {
                center[a.length() - 1 - j] = t+jing;
                jing = 0;
            }

        }
        if (jing)//判断最后一位是否需要进位
        {
            center[a.length()] = jing;
            jing = 0;
            logo = 1;
        }
        for (k = m; k < a.length() + m + logo; k++)
        {
            l = result[k] + center[k - m];
            if (l >= 10)
            {
                result[k] = l - 10;
                result[k + 1]++;
            }
            else
                result[k] = l;
        }
        m++;
    }
    for (i = 4009; i >= 0; i--)//逆序找出第一个不为0的位置
    {
        if (result[i] != 0)
        {
            j = i;
            break;
        }

    }
    if (i == -1)
        cout << 0;//为了输出0*0情况
    for (; j >= 0; j--)
        cout << result[j];//输出结果
    return 0;
}

by jjb_coder @ 2020-08-11 23:27:26

解决了,加法出错


|