【POJ - 2389】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

题意:

给两个数,求出乘积。

解题报告:

题目中给的两个数都很大,需要用大数乘法。需要注意前导0别输出。写完后可以当做板子记住,以后可能还会用。感觉大数乘法就是模拟了,乘法的过程,结果需要用数组存下来,输出要注意细节,存的方式可以数组的一位就存一个数,也可以数组的一位存多位数。

ac代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring> 
#include<queue>
#include<stack> 
#include<string.h>
#define ll long long
using namespace std;

char n[800],m[800];int ans[1000000];
int main()
{
//	cinline()
	scanf("%s",n);
	scanf("%s",m);
	int ln=strlen(n);
	int lm=strlen(m);
	reverse(n,n+ln);
	reverse(m,m+lm);//可能忘了反转了,把0号位当成低位了。。。 
	int l1=0;
	for(int i=0;i<lm;i++)
	{
		int st=i;
		for(int j=0;j<ln;j++)
		{
			int t1=(m[i]-'0')*(n[j]-'0');//%10;
			ans[st+j]+=t1;
			if(ans[st+j]>9)
			{
				ans[st+j+1]+=ans[st+j]/10;
				ans[st+j]=ans[st+j]%10;
				l1=max(l1,st+j+1);
			}
			l1=max(l1,st+j);
		}
	}
	int flag=0;
	for(int i=l1;i>=0;i--)
	if(ans[i]!=0&&flag==0)
	{
		flag=1; //一开始没写后缀0没了 如  0*0无输出 
		printf("%d",ans[i]);
	}
	else if(flag||i==0)
	 printf("%d",ans[i]);
	printf("\n");	
	return 0;
 } 

 套的大佬的板子:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring> 
#include<queue>
#include<stack> 
#include<string.h>
#define ll long long
using namespace std;
struct BigInt {
	const static int mod = 10000;
	const static int DLEN = 4;
	int a[600],len;
	BigInt() {
		memset(a,0,sizeof(a));
		len = 1;
	}
	BigInt(int v) {
		memset(a,0,sizeof(a));
		len = 0;
		do {
			a[len++] = v%mod;
			v /= mod;
		} while(v);
	}
	BigInt(const char s[]) {
		memset(a,0,sizeof(a));
		int L = strlen(s);
		len = L/DLEN;
		if(L%DLEN)len++;
		int index = 0;
		for(int i = L - 1; i >= 0; i -= DLEN) {
			int t = 0;
			int k = i - DLEN + 1;
			if(k < 0)k = 0;
			for(int j = k; j <= i; j++)
				t = t*10 + s[j] - '0';
			a[index++] = t;
		}
	}
	BigInt operator +(const BigInt &b)const {
		BigInt res;
		res.len = max(len,b.len);
		for(int i = 0; i <= res.len; i++)
			res.a[i] = 0;
		for(int i = 0; i < res.len; i++) {
			res.a[i] += ((i < len)?a[i]:0)+((i < b.len)?b.a[i]:0);
			res.a[i+1] += res.a[i]/mod;
			res.a[i] %= mod;
		}
		if(res.a[res.len] > 0)res.len++;
		return res;
	}
	BigInt operator *(const BigInt &b)const {
		BigInt res;
		for(int i = 0; i < len; i++) {
			int up = 0;
			for(int j = 0; j < b.len; j++) {
				int temp = a[i]*b.a[j] + res.a[i+j] + up;
				res.a[i+j] = temp%mod;
				up = temp/mod;
			}
			if(up != 0)
				res.a[i + b.len] = up;
		}
		res.len = len + b.len;
		while(res.a[res.len - 1] == 0 &&res.len > 1)res.len -- ;
		return res;
	}
	void output() {
		printf("%d",a[len - 1]);
		for(int i = len - 2; i >=0 ; i -- )
			printf("%04d",a[i]);
		printf("\n");
	}
};
char n[800],m[800];
int main()
{
	scanf("%s%s",n,m);
	BigInt a(n),b(m),ans;
	ans=a.operator *(b);
	ans.output();
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值