码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2389.Bull Math 解题报告

时间:2015-01-02 22:18:09      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://poj.org/problem?id=2389

题目意思:就是大整数乘法。

  题目中说每个整数不超过 40 位,是错的!!!要开大点,这里我开到100.

  其实大整数乘法还是第一次写 = =.......大整数加法写得比较多。百练也有一条是大整数乘法,链接如下:http://bailian.openjudge.cn/practice/2980/

  一步一步模拟即可,代码就是按这个来写的。

  以 835 * 49 为例(亲爱的读者,允许我截图吧)

 技术分享

       简直就是神奇呀~~~~~

   

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int maxn = 100 + 10;
 8 char ca[maxn], cb[maxn];
 9 int ia[maxn], ib[maxn];
10 int res[2*maxn+10];
11 
12 int main()
13 {
14     #ifndef ONLINE_JUDGE
15         freopen("in.txt", "r", stdin);
16     #endif // ONLINE_JUDGE
17     while (scanf("%s%s", ca, cb) != EOF)
18     {
19         int l1 = strlen(ca);
20         for (int i = 0; i < l1; i++)
21             ia[l1-i-1] = ca[i] - 0;
22         int l2 = strlen(cb);
23         for (int i = 0; i < l2; i++)
24             ib[l2-i-1] = cb[i] - 0;
25         memset(res, 0, sizeof(res));
26         for (int i = 0; i < l1; i++)
27         {
28             for (int j = 0; j < l2; j++)
29                 res[i+j] += ia[i] * ib[j];
30         }
31         // 处理进位问题
32         for (int i = 0; i < l1+l2; i++)
33         {
34             if (res[i] >= 10)
35             {
36                 res[i+1] += res[i] / 10;
37                 res[i] %= 10;
38             }
39         }
40         bool flag = false;
41         for (int i = maxn; i >= 0; i--)
42         {
43             if (flag)
44                 printf("%d", res[i]);
45             else if (res[i])
46             {
47                 printf("%d", res[i]);
48                 flag = true;
49             }
50         }
51         if (!flag)
52             printf("0");
53         printf("\n");
54     }
55     return 0;
56 }

 

  

poj 2389.Bull Math 解题报告

标签:

原文地址:http://www.cnblogs.com/windysai/p/4198859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!