60求助!!!

P1303 A*B Problem

qb1_1 @ 2022-11-18 19:56:32


#include <bits/stdc++.h>
using namespace std;
char t[ 1000 ]  , k[ 1000 ];
int a[ 1000 ] , b[ 1000 ] , c[ 1000 ];
int main(){
    scanf (" %s %s ", t + 1 , k + 1 );
    int rt = strlen(t+1),tr=strlen( k + 1 );
    int asd = rt + tr - 1;
    for ( int i = 1;i <= rt ; ++i )
        a[ i ] = t[ rt - i + 1 ]-'0';
    for ( int  i = 1 ; i <= tr ; ++i )
        b[ i ]=k[ tr - i + 1 ]-'0';
    for ( int i = 1;i <= rt; ++i )
        for ( int j = 1;j <= tr ; ++j )
            c [ i + j - 1 ] += a[ i ] * b[ j ];
                for( int i = 1; i <= asd; ++i ){
                    if ( c[ i ] >= 10 ){
                        c[ i + 1 ]+=c[ i ] / 10;
                            c[ i ] %= 10;
    }
}
    if ( c [ asd + 1 ] )
        asd++;  
    for ( int i = asd; i >= 1; i-- )
    printf ( "%d", c[ i ] );
    return 0;
}

by _O__o_ @ 2022-11-18 20:11:41

#include<bits/stdc++.h>

using namespace std;
char a[10001];
char b[10001];
int a1[10001],b1[10001];
int c[20002];
int main() {
    cin >>a >> b;
    int lena = strlen(a);
    int lenb = strlen(b);
    for(int i = 0;i < lena;i++) a1[i] = a[lena - 1- i] - '0';
    for(int i = 0;i < lenb;i++) b1[i] = b[lenb - 1- i] - '0';
    for(int i = 0;i < lena;i++){
        int x = 0;
        for(int j = 0;j < lenb;j++){
            c[i + j] += a1[ i ]  * b1[j] + x;
                x     = c[i + j] / 10;
            c[i + j]  = c[i + j] % 10;    
            c[i+j+1] += c[i + j] / 10;      
        }
        if(x > 0) c[i + lenb] += x;
    }
    int lenc = lena + lenb;
    while(lenc > 1 && c[lenc - 1] == 0) lenc--;
    for(int i = 0;i < lenc;i++) cout << c[lenc - 1- i];

    return 0;
}

|