【50 笔试真题 C++】大整数相乘

版权声明:欢迎转载,请注明来源 https://blog.csdn.net/linghugoolge/article/details/87870061

[编程题] 大整数相乘 

时间限制:1秒 
空间限制:32768K 
有两个用字符串表示的非常大的大整数,算出他们的乘积,也是用字符串表示。不能用系统自带的大整数类型。 
输入描述: 
空格分隔的两个字符串,代表输入的两个大整数

输出描述: 
输入的乘积,用字符串表示

输入例子1: 
72106547548473106236 982161082972751393

输出例子1: 
70820244829634538040848656466105986748

代码

没有刷题经验,真的一脸懵逼

#include<iostream>
#include<string>
#include<algorithm>
#define N 11000
using namespace std;
int main(){
    string s1,s2;
    cin>>s1>>s2;
    
    reverse(s1.begin(),s1.end());
    reverse(s2.begin(),s2.end());
    
    int a[N]={0};
    for(int i=0;i<s1.length();++i){
        for(int j=0;j<s2.length();++j){
            a[i+j]+=(s1[i]-'0')*(s2[j]-'0');
        }
    }
    
    for(int i=0;i<N-1;++i){
        a[i+1]+=a[i]/10;
        a[i]=a[i]%10;
    }
    
    int i=N-1;
    while(a[i]==0)
        i--;
    for(int j=i;j>=0;--j)
        cout<<a[j];
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/linghugoolge/article/details/87870061