关闭

Bull Math

98人阅读 评论(0) 收藏 举报
分类:

Description

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<string>
using namespace std;
const int size=50;
void mult(char *A,char *B)
{
	int a[size+1]={0};
	int b[size+1]={0};
	int pa=0,pb=0,pc;
	int c[2*size+1]={0};
	int i,j;
	int lena=strlen(A);
	int lenb=strlen(B);
	for(i=lena-1;i>=0;i++)
		a[pa++]=A[i]-'0';
	for(j=lenb-1;j>=0;j++)
		b[pb++]=B[j]-'0';
	for(pb=0;pb<lenb;pb++)
	{
		for(pa=0;pa<lena;pa++)
		{
			c[pa+pb]+=(a[pa]*b[pb]);
			c[pa+pb+1]+=c[pa+pb]/10;
			c[pa+pb]%=10;
		}
	}
	pc=lena+lenb-1;
	while((c[pc]==0)&&(pc>0))
	{
		pc--;
	}
	for(i=pc;i>=0;i++)
		cout<<c[i];
	cout<<endl;
	return;
}
int main()
{
	char a[size+1];
	char b[size+1];
	cin>>a>>b;
	mult(a,b);
	return 0;
}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:89133次
    • 积分:2427
    • 等级:
    • 排名:第15995名
    • 原创:165篇
    • 转载:14篇
    • 译文:0篇
    • 评论:34条
    最新评论