40分求解

P1303 A*B Problem

CiderLee @ 2018-02-07 20:56:36

用了高精和没用一样……

include <iostream>

include <cstring>

include <cstdio>

using namespace std; int c[10000],d[10000]; char a[10000],b[10000]; int temp[10000],wancheng[10000]; int main() { // freopen("testdata.in","r",stdin); int i,j,head=0,lenc,tail=0,flag=1; cin>>a>>b; if(a[0]=='0'||b[0]=='0') { cout<<0<<endl; return 0; } for(i=strlen(a)-1;i>=0;i--) { if(a[i]!='-') c[head++]=a[i]-'0'; } head=0; for(i=strlen(b)-1;i>=0;i--) { if(b[i]!='-') d[head++]=b[i]-'0'; } if(a[0]=='-') { if(b[0]!='-') cout<<'-'; } if(a[0]!='-') { if(b[0]=='-') cout<<'-'; } for(i=0;i<max(strlen(a),strlen(b));i++) { for(j=0;j<min(strlen(a),strlen(b));j++) { temp[i+j]+=c[i]d[j]; } } for(i=0;i<=max(strlen(a),strlen(b))2;i++) { if(temp[i]>9) { wancheng[tail++]=temp[i]%10; temp[i]/=10; while(temp[i]) { temp[i+1]+=temp[i]%10; temp[i]/=10; } } else { wancheng[tail++]=temp[i]%10; } } for(i=max(strlen(a),strlen(b))*2;i>=0;i--) if(wancheng[i]==0&&flag==1) continue; else { flag=0; cout<<wancheng[i]; } return 0; }


by CiderLee @ 2018-02-07 20:58:08

#include <iostream>
#include <cstring>
using namespace std;
int c[10000],d[10000];
char a[10000],b[10000];
int temp[10000],wancheng[10000];
int main()
{
    int i,j,head=0,lenc,tail=0,flag=1;
    cin>>a>>b;
    if(a[0]=='0'||b[0]=='0')
    {
        cout<<0<<endl;
        return 0;
    }
    for(i=strlen(a)-1;i>=0;i--)
    {
        if(a[i]!='-')
            c[head++]=a[i]-'0';
    }
    head=0;
    for(i=strlen(b)-1;i>=0;i--)
    {
        if(b[i]!='-')
            d[head++]=b[i]-'0';
    }
    if(a[0]=='-')
    {
        if(b[0]!='-')
        cout<<'-';
    }
    if(a[0]!='-')
    {
        if(b[0]=='-')
        cout<<'-';
    }
    for(i=0;i<max(strlen(a),strlen(b));i++)
    {
        for(j=0;j<min(strlen(a),strlen(b));j++)
        {
            temp[i+j]+=c[i]*d[j];
        }
    }
    for(i=0;i<=max(strlen(a),strlen(b))*2;i++)
    {
            if(temp[i]>9)
            {
                wancheng[tail++]=temp[i]%10;
                temp[i]/=10;
                while(temp[i])
                {
                    temp[i+1]+=temp[i]%10;
                    temp[i]/=10;
                }
            }
            else
            {
                wancheng[tail++]=temp[i]%10;
            }
    }
    for(i=max(strlen(a),strlen(b))*2;i>=0;i--)
        if(wancheng[i]==0&&flag==1)
            continue;
        else
        {
            flag=0;
            cout<<wancheng[i];
        }
    return 0;
}

|