为什么输出会比答案长,求大佬!!!!

P1303 A*B Problem

theshyer @ 2021-03-21 14:31:46

#include<bits/stdc++.h>

using namespace std;

int aall[2000];
int ball[2000];
int c[5010] ;

int main()
{
    ios::sync_with_stdio(false);
    string a,b;
    cin >> a >> b;
    int a1 = a.length();
    int b1 = b.length();
    for (int i = 0,j = a1-1;j>=0;i++,j--)
    {
        aall[i] = a[j] - '0';
    }
    for (int i = 0,j = b1-1;j>=0;i++,j--)
    {
        ball[i] = b[j] - '0';
    }
    for(int i = 0;i<a1;i++)
    {
        for(int j = 0;j<b1;j++)
        {
            c[i+j] += aall[i]*ball[j];
        }
    }
    for (int i = 0;i<a1+b1;i++)
    {
        c[i+1] += c[i] /10;
        c[i] %= 10;
    }
    int len = a1+b1;
    if(c[len-1] == 0)
        len--;
    for (int i = max(0,len-1);i>=0;i--)
    {
       printf("%d",c[i]);
    }
    return 0;
}

by Leap_Frog @ 2021-03-21 14:37:14

用户名辱骂,陶片见


by hanyuchen2019 @ 2021-03-21 14:37:41

已举报


by alvis @ 2021-03-21 14:50:26

去除前导零的时候应该用while

while(c[len-1] == 0)len --;

(lz改名了


by theshyer @ 2021-03-21 14:55:40

@nade 为什么? 不是只要去掉前面哪一个就可以了吗??


by alvis @ 2021-03-21 20:46:00

az,按他给的数据0 * 10 来看,a1 = 1,b1 = 2,所以len 为 3,如果只删去一个前导零,很显然会多输出。(你可以自己手敲一组数据来验证


by theshyer @ 2021-03-21 21:48:12

@nade 谢了


|