UVA10106 Product

本文介绍了一种解决大整数相乘问题的高精度乘法算法实现方案,通过定义结构体来存储多位数,并使用逐位相乘及进位处理的方法完成运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

高精度乘法

题目:

Product

 

 Product 

 

The Problem

The problem is to multiply two integers X, Y. (0<=X,Y<10250)

The Input

The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.

The Output

For each input pair of lines the output line should consist one integer the product.

Sample Input

 

12
12
2
222222222222222222222222

 

Sample Output

 

144
444444444444444444444444

解答:

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 struct bign
 4 {
 5     int len;
 6     int s[1000];
 7 };
 8 bign a,b,res;
 9 void change(char *str,bign *b)
10 {
11     int i;
12     b->len=strlen(str);
13     for(i=0;i<b->len;i++)
14         b->s[i]=str[b->len-i-1]-'0';
15 }
16 void mult(bign *a,bign *b,bign *res)
17 {
18     res->len=a->len+b->len;
19     int i,j;
20     for(i=0;i<a->len;i++)
21     {
22         for(j=0;j<b->len;j++)
23         {
24             res->s[i+j]+=a->s[i]*b->s[j];
25         }
26     }
27     for(i=0;i<res->len-1;i++)
28     {
29         res->s[i+1]+=res->s[i]/10;
30         res->s[i]%=10;
31     }
32     while(res->len>1&&res->s[res->len-1]==0)
33         res->len--;
34 }
35 char s1[1000],s2[1000];
36 int main()
37 {
38     while(scanf("%s%s",s1,s2)!=EOF)
39     {
40         memset(res.s,0,sizeof(res.s));
41         change(s1,&a);
42         change(s2,&b);
43         mult(&a,&b,&res);
44         int i;
45         for(i=res.len-1;i>=0;i--)
46             printf("%d",res.s[i]);
47         printf("\n");
48     }
49     return 0;
50 }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值