• poj 2389.Bull Math 解题报告


    题目链接: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("
    ");
    54     }
    55     return 0;
    56 }

      

  • 相关阅读:
    使用require.context引入模块
    npm link的使用
    mysql 链接远程数据库
    微信错误:errcode=45015, errmsg=response out of time limit or subscription is canceled
    微信公众号发送模板消息
    VS CODE 开发php实现断点调试
    uni 蓝牙 安卓 监听不到返回数据 不可写入
    vue3.0 兄弟组件传值
    二叉查找树的实现和删除
    模块二:ES新特性与TypeScript、JS性能优化
  • 原文地址:https://www.cnblogs.com/windysai/p/4198859.html
Copyright © 2020-2023  润新知