第一点过不去

P1303 A*B Problem

lonlyn @ 2016-08-14 15:12:00

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s1[5000],s2[5000],s3[5000];
int a[5000],b[5000],c[10000];
int la,lb,lc;
bool flag=false;
void fc()
{
    if (!flag) flag=true;
    else if (flag) flag=false;
}
int main()
{
    scanf("%s",s1);
    scanf("%s",s2);
    la=strlen(s1); lb=strlen(s2);
    if (s1[0]=='-')
    {
        fc();
        --la;
    }
    if (s2[0]=='-')
    {
        fc();
        --lb;
    }
    for (int i=1;i<=la;++i)
    {
        if (s1[0]=='-') a[i]=s1[la-i+1]-'0';
        else
        a[i]=s1[la-i]-'0';
    }
    for (int i=1;i<=lb;++i)
    {
        if (s2[0]=='-') b[i]=s2[lb-i+1]-'0';
        else
        b[i]=s2[lb-i]-'0'; 
    }
    for (int i=1;i<=la;++i)
        for (int j=1;j<=lb;++j)
        {
            c[i+j-1]=c[i+j-1]+a[i]*b[j];
            c[i+j]=c[i+j]+c[i+j-1]/10;
            c[i+j-1]=c[i+j-1]%10;    
        }
    lc=la+lb;
    while (!c[lc]) lc--;
    if (flag) cout<<'-';
    for (int i=lc;i>=1;--i)
        cout<<c[i];
}
**Too many or to few lines?******

by yzz2016 @ 2016-08-14 18:24:36

我也是


by 诺诺_陈墨瞳 @ 2016-08-18 17:11:21

第一个点有一个数为0的


by lzr20040310 @ 2016-09-10 13:00:43

去前导0时要注意结果是否为0,普通的去前导0在结果为0是会空输出(clen=0)


by frankchenfu @ 2016-10-07 14:10:45

最后删0的时候改成 while ( ! c [lc] && lc > 1 )


by 为彼此是艳火 @ 2017-05-18 13:49:01

第一个点是0,,删除前导0时注意别删空了.


|