萌新求助,为什么有的结果准确有的不

P1303 A*B Problem

gyhwhitenight @ 2024-01-26 02:46:21

#include<iostream>
#include<cstring>
using namespace std;
int main() {
    char a1[2001] = " ", b1[2001] = " ";
    int len1 = 0, len2 = 0,t1=0,t2=0,t3=0;
    int a[2010] = { 0 }, b[2010] = { 0 }, ans[10001] = { 0 }, m[2001][2001] = { 0 };
    cin >> a1 >> b1;
    len1 = strlen(a1);
    len2 = strlen(b1);
    if (a1[0] == '0' || b1[0] == '0') {
        cout << "0 " << endl;
        return 0;
    }
    for (int i = 0; i < len1; i++) {
        a[i] = a1[i] - '0';
    }
    for (int i = 0; i < len2; i++) {
        b[i] = b1[i] - '0';
    }
    for (int i = len1; i >0; i--) {
        a[i] = a[i-1];
    }
    a[0] = 0;
    for (int i = len2; i > 0; i--) {
        b[i] = b[i - 1];
    }
    b[0] = 0;
    for (int i = 0; i < len2; i++) {
        t1 = 0;
        for (int j = len1; j >= 0; j--) {
            m[i][j] = (a[j] * b[len2-i] + t1) % 10;
            t1= (a[j] * b[len2-i] + t1) / 10;
        }
    }
    for (int i = len2 - 1; i >= 0; i--) {
        for (int j = 0; j < len2 - 1 - i; j++) {
            for (int q = len1+1; q >0; q--) {
                m[i][j + q] = m[i][j + q - 1];
            }
            m[i][j] = 0;
        }
    }
    for (int i = 0; i < len2; i++) {
        t3 = 0; 
        for (int j = len1 + len2; j >= 0; j--) {
            ans[j] = (ans[j] + m[i][j] + t3) % 10;
            t3 = (ans[j] + m[i][j] + t3) / 10;
        }
    }
    int c = 0;
    for (int i = 0; i <= len1 + len2-1; i++) {
        if (ans[i] == 0 && c == 0) {
            continue;
        }
        else {
            if (ans[i] != 0) {
                cout << ans[i];
                c = 1;
            }
            else {
                cout << ans[i];
            }
        }
    }
    return 0;
}

by Miracle_InDream @ 2024-01-26 07:21:18

#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int a[2005],b[2005],c[20005];

int main()
{
    cin>>s1>>s2;
    a[0]=s1.size();
    b[0]=s2.size();
    for(int i=1;i<=a[0];i++)
    {
        a[i]=s1[a[0]-i]-'0';
    }
    for(int i=1;i<=b[0];i++)
    {
        b[i]=s2[b[0]-i]-'0';
    }
    c[0]=a[0]+b[0];
        int jw;
        for(int i=1;i<=b[0];i++)
        {
            jw=0;
            for(int j=1;j<=a[0];j++)
            {
                c[i+j-1]+=a[j]*b[i]+jw;
                jw=c[i+j-1]/10;
                c[i+j-1]%=10;
            }
            c[i+a[0]]=jw;
        }
        while(c[c[0]]==0&&c[0]>1)
        {
            c[0]--;
        }
    for(int i=c[0];i>=1;i--) cout<<c[i];
    return 0;
}

by Miracle_InDream @ 2024-01-26 07:21:28

@gyhwhitenight


by Arkeris @ 2024-01-26 07:46:57

@shooting__star 他在求调,你发个代码干什么?


|