Bull Math (大数乘模拟)

Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls' answers. Read in two positive integers (no more than 40 digits each) and compute their product. Output it as a normal number (with no extra leading zeros). 

FJ asks that you do this yourself; don't use a special library function for the multiplication.

Input

* Lines 1..2: Each line contains a single decimal number.

Output

* Line 1: The exact product of the two input lines

Sample Input

11111111111111
1111111111

Sample Output

12345679011110987654321
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int a[105],b[105],ans[105];
int lena=0,lenb=0;
void mult(string s1,string s2){
    for(int i=s1.length()-1 ;i>=0;i--){//将数逆转 
    	a[lena]=s1[i]-'0';
    	lena++;
	}
    for(int i=s2.length()-1 ;i>=0;i--){
    	b[lenb]=s2[i]-'0';
    	lenb++;
	}
	for(int i=0;i<lena;i++){
		for(int j=0;j<lenb;j++){
			ans[i+j]+=a[i]*b[j];//原来的进位加上又新乘出来的 
			ans[i+j+1]+=ans[i+j]/10;//把进位添到上一位去 
			ans[i+j]=ans[i+j]%10;//除去进位 
		}
	}
	bool flag=true;
	for(int i=100;i>=0;i--){
		if(ans[i]!=0||!flag){
			flag=false;
			cout<<ans[i];
		}
	} 
	cout<<endl;
}
int main(){
	string s1,s2;
        cin>>s1>>s2;
	mult(s1,s2);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/red_red_red/article/details/88088081