zoukankan      html  css  js  c++  java
  • UVa 10106 Product

    高精度乘法问题,WA了两次是因为没有考虑结果为0的情况。



     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

    AC代码:

     1 //#define LOCAL
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 const int maxn = 265;
     8 char a[maxn], b[maxn];
     9 int x[maxn], y[maxn], mul[maxn * 2 + 10];
    10 void Reverse(char s[], int l);
    11 
    12 int main(void)
    13 {
    14     #ifdef LOCAL
    15         freopen("10106in.txt", "r", stdin);
    16     #endif
    17 
    18     while(gets(a))
    19     {
    20         gets(b);
    21         int la = strlen(a);
    22         int lb = strlen(b);
    23         memset(mul, 0, sizeof(mul));
    24         memset(x, 0, sizeof(x));
    25         memset(y, 0, sizeof(y));
    26         Reverse(a, la);
    27         Reverse(b, lb);
    28         int i, j;
    29         for(i = 0; i < la; ++i)
    30             x[i] = a[i] - '0';
    31         for(i = 0; i < lb; ++i)
    32             y[i] = b[i] - '0';
    33 
    34         for(i = 0; i < lb; ++i)
    35         {
    36             int s = 0, c = 0;
    37             for(j = 0; j < maxn; ++j)
    38             {
    39                 s = y[i] * x[j] + c + mul[i + j];
    40                 mul[i + j] = s % 10;
    41                 c = s / 10;
    42             }
    43         }
    44 
    45         for(i = maxn * 2 + 9; i >= 0; --i)
    46             if(mul[i] != 0)
    47                 break;
    48         if(i < 0)
    49             cout << 0;
    50         else
    51         {
    52             for(; i >=0; --i)
    53                 cout << mul[i];
    54         }
    55         cout << endl;
    56     }
    57     return 0;
    58 }
    59 //用来反转数组
    60 void Reverse(char s[], int l)
    61 {
    62     int i;
    63     char t;
    64     for(i = 0; i < l / 2; ++i)
    65     {
    66         t = s[i];
    67         s[i] = s[l - i -1];
    68         s[l - i -1] = t;
    69     }
    70 }
    代码君
  • 相关阅读:
    命令执行顺序控制与管道
    js获取返回首页
    手机站点击商务通无轨迹解决方法
    js文字向上滚动代码
    文字隐藏多余的文字
    QQ弹窗代码
    百度推送代码
    js手机站跳转
    js 判断时间,满足执行框架
    js切换换class
  • 原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/3817598.html
Copyright © 2011-2022 走看看