poj2389 Bull Math (高精度之A*B)

题目来源:poj2389

Bull Math
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13350 Accepted: 6873

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

解析:假设用字符数组读入两个超长数a、b,用int c[] 数组记录他们的乘积。

           len_a表示字符数组 a 的长度,a[len_a-i]表示字符数组 a 所代表数字从个位往高位方向的第几位,如:a[len_a-1]代表数字a的个位,a[len_a-2]代表数字a的十位,等等。b数组相关定义与此类似。

           c[k]表示数字c从个位往高位方向的第几位,比如c[1]代表数字c的个位,c[2]代表数字c的十位,等等。

           c[k]=∑(a[len_a-i]-'0')*(b[len_b-(k+1-i)]-'0')       (i=1——>len_a ,1<=k+1-i<=len_b);  

代码:

#include<cstdio>
#include<cstring>
#define maxn 250
using namespace std;

char a[maxn+50],b[maxn+50];
int c[maxn*maxn+100];

void redirect()
{
  freopen("1.in","r",stdin);
  freopen("1.out","w",stdout);
}

void work()
{
  int i,j,k,last=0,len1=strlen(a),len2=strlen(b);
  c[0]=len1+len2-1;
  for(k=1;k<=c[0];k++)
    {
      c[k]=last;
      for(i=1;i<=len1;i++)
        if(k+1-i>=1 && k+1-i<=len2)
          c[k]+=(a[len1-i]-'0')*(b[len2-(k+1-i)]-'0');
      last=c[k]/10,c[k]%=10;		   
	}
  
  if(last>0)printf("%d",last);
  for(k=c[0];k>=1;k--)printf("%d",c[k]);
  printf("\n");	
  
}

int main()
{
  redirect();
  while(gets(a)!=NULL)
    gets(b),work();
  return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值