大数相乘

#include<iostream>
#include<cstring>

using namespace std;

void multiply(const char* a, const char* b){
	int aLen = strlen(a);
	int bLen = strlen(b);
	int *result = new int[aLen+bLen];
	for(int i=0; i<(aLen+bLen); i++)
		result[i]=0;
	for(int i=0; i<aLen; i++)
		for(int j=0;j<bLen; j++){
			result[i+j+1]+=(a[i]-'0')*(b[j]-'0');
		}
			
	for(int i=(aLen+bLen-1); i>=0; i--){
		if(result[i]>=10){
			result[i-1]+=result[i]/10;
			result[i]%=10;
		}
	}
	int i=0;
	while(result[i]==0) i++;
	for(int j=i; j<(aLen+bLen); j++)
		cout<<result[j];
	cout<<endl;
	
	
}

int main(){
	string a,b;
	cin>>a>>b;
	const char *num1=a.c_str();
	const char *num2=b.c_str();
	multiply(num1,num2);
	return 0;
}

你可能感兴趣的:(大数相乘)