您的位置:首页 > 其它

【bzoj1754/Usaco2005 qua】Bull Math——高精度乘法

2017-10-13 20:56 211 查看

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<cstdio>
#include<cstring>
#include<cmath>
char b1[45],a1[45];
int a[45],b[45],ans[100];
int main(){
scanf("%s %s",a1+1,b1+1);
a[0]=strlen(a1+1);b[0]=strlen(b1+1);
for(int i=1;i<=a[0];i++)a[i]=a1[a[0]-i+1]-48;
for(int j=1;j<=b[0];j++)b[j]=b1[b[0]-j+1]-48;
for(int i=1;i<=a[0];i++){
for(int j=1;j<=b[0];j++){
ans[i+j-1]+=a[i]*b[j];
ans[i+j]+=ans[i+j-1]/10;
ans[i+j-1]%=10;
}
}
ans[0]=a[0]+b[0];
while(!ans[ans[0]]&&ans[0]>1)ans[0]--;
for(int i=ans[0];i>=1;i--)printf("%d",ans[i]);
return 0;
}
bzoj1754

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: