【bzoj 1754】【POJ - 2389 】Bull Math (高精度运算)

27 篇文章 0 订阅
16 篇文章 0 订阅

题干:

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

解题报告:

      好多错误啊一开始写的时候。就是算个大数乘法。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int MAX =1000 + 5 ;
char s1[MAX],s2[MAX];
int a[MAX],b[MAX],ans[MAX];
int len1,len2;
int p,c;//p代表当前位置,c代表进位 
int main()
{
//	freopen("in.txt","r",stdin);
	scanf("%s",s1);
	scanf("%s",s2);
	len1=strlen(s1);len2=strlen(s2);
	//
	for(int i = 1; i<=len1; i++) {
		a[len1-(i-1)]=s1[i-1]-'0';
	}
	for(int i = 1; i<=len2; i++) {
		b[len2-(i-1)]=s2[i-1]-'0';
	}
//	for(int i = len1; i>=1; i--) printf("%d",a[i]);
//	printf("\n");
//	for(int i = len2; i>=1; i--) printf("%d",b[i]);
//	printf("\n");
	for(int i = 1; i<=len2; i++) {
		for(int j = 1; j<=len1; j++) {
			ans[i+j-1] += b[i]*a[j];
		} 
	}
	for(int i = 1; i<=len1+len2; i++) {
		p=i;
		if(ans[i]<=9) continue;
//		c=ans[i]/10;
//		while(c>0) {
//			ans[++p] += c;
//			ans[p-1]=ans[p-1]%10;
//			c= ( c+ans[p] )/10;
//			
//		}
		while(ans[i]>9) {
			ans[i]-=10;
			ans[i+1]++;
		} 
	}

	
//	printf("p = %d\n",p);
	for(int i = len1+len2; i>=1; i--) {
		if(ans[i]!=0) {
			p=i;break;
		}
	}
	for(int i = p; i>=1; i--) {
		printf("%d",ans[i]);
	}
	printf("\n");
	
	return 0 ;
 } 
 //bzoj 1754 [Usaco2005 qua]Bull Math
 //11111111111111
 //1111111111
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值