*foj 1453 Bignum Arithmetic
2015-05-01 19:29
309 查看
Problem Description
In this problem, you will be concerned with integers with very large numbers of digits. You must write code which will repeatedly accept (until end of file) two lines each containing an unsigned integer, and output the product of the two input unsigned integers. The output must not contain any leading zeros.
You can assume that each integer will contain at most 80 digits. The input ends with an end of file.
Sample Input
0342
1298
12
3
Sample Output
443916
36
代码:
In this problem, you will be concerned with integers with very large numbers of digits. You must write code which will repeatedly accept (until end of file) two lines each containing an unsigned integer, and output the product of the two input unsigned integers. The output must not contain any leading zeros.
You can assume that each integer will contain at most 80 digits. The input ends with an end of file.
Sample Input
0342
1298
12
3
Sample Output
443916
36
代码:
#include<stdio.h> #include<string.h> char s1[1005],s2[1005]; int a[1005],b[1005],c[1500]; void mul(char s1[],char s2[]) { int l1,l2; l1=strlen(s1),l2=strlen(s2); int l=l1+l2; int i,j,k=0; for(i=l1-1,j=0;i>=0;i--,j++)a[j]=s1[i]-'0'; for(i=l2-1,j=0;i>=0;i--,j++)b[j]=s2[i]-'0'; memset(c,0,sizeof(c));//对数组c初始化 for(i=0;i<l1;i++) for(j=0;j<l2;j++) c[i+j]+=a[i]*b[j]; for(i=0;i<l;i++) if(c[i]>9) c[i+1]+=c[i]/10,c[i]%=10; while(c[l]==0&&l>0)//*去掉前导0,但保证最后有一个0!!否则结果为0不能输出 l--; for(i=l;i>=0;i--) printf("%d",c[i]); printf("\n"); } int main() { while(scanf("%s",s1)!=EOF) { getchar();//吞掉回车 gets(s2); mul(s1,s2); } return 0; }
相关文章推荐
- UVa 1453 - Squares 旋转卡壳求凸包直径
- uva 1453 Squares (旋转卡壳 rotating calipers)
- 51nod 1453:抽彩球
- hiho1453 Rikka with Tree 01背包
- 51nod 1453 抽彩球(组合数)
- uva1453 旋转卡壳算法
- ZOJ 1453 Surround the Trees(凸包入门:求凸包周长)
- 【WC2005】【BZOJ1453】Dface双面棋盘
- bzoj1453: [Wc]Dface双面棋盘
- 1453: [Wc]Dface双面棋盘 (线段树+并茶几)
- UVA 1453 - Squares
- BZOJ1453 : [Wc]Dface双面棋盘
- BZOJ 1453 [WC] 双面棋盘 并查集+线段树暴搞
- [CDQ分治 并查集] BZOJ 1453 [Wc]Dface双面棋盘
- 51Nod 1453 抽彩球
- UVA 1453 - Squares
- 九度 题目1453:Greedy Tino
- ZOJ - 1453 —— Surround the Trees (求凸包的周长)
- BZOJ1453: [Wc]Dface双面棋盘
- 【1453】筛法