2020-07-21 A*B Problem

2020-07-21  本文已影响0人  JalorOo

https://www.luogu.com.cn/problem/P1303

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;

int read(){
    int x = 0,f = 1;
    char c = getchar();
    while (c<'0'||c>'9') {
        if (c=='-') {
            f = -1;
        }
        c = getchar();
    }
    while (c>='0'&&c<='9') {
        x = x*10+c-'0';
        c = getchar();
    }
    return x*f;
}

int a[2005],b[2005],c[10001];

int main ()
{
    string a1,b1;
    cin>>a1>>b1;
    
    int lena = a1.length();//字符串长度
    int lenb = b1.length();//字符串长度
    
    for(int i=1;i<=lena;i++){
        a[i]=a1[lena-i]-'0';
    }
    
    for(int i=1;i<=lenb;i++){
        b[i]=b1[lenb-i]-'0';
    }
    
    for(int i=1;i<=lenb;i++){
        for(int j=1;j<=lena;j++){
            c[i+j-1]+=a[j]*b[i];
        }
    }
    
    for(int i=1;i<lena+lenb;i++){
        if(c[i]>9){
            c[i+1]+=c[i]/10;
            c[i]%=10;
        }
    }
    
    int len=lena+lenb;
    
    while(c[len]==0&&len>1){
        len--;
    }
    
    for(int i=len;i>=1;i--){
        cout<<c[i];
    }
    
    return 0;
}
/*
1000000000000000000000
199
 
 46546876443156448001
 46453168410002134684
 93000044853158582685
*/

上一篇 下一篇

猜你喜欢

热点阅读