*foj 1453 Bignum Arithmetic

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

代码:

#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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值