UVa 10106 Product

來源:互聯網
上載者:User

標籤:style   blog   http   color   strong   os   

高精度乘法問題,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

 

12122222222222222222222222222

 

Sample Output

 

144444444444444444444444444

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 LOCAL15         freopen("10106in.txt", "r", stdin);16     #endif17 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         else51         {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 }
代碼君

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.