Product

来源:互联网 发布:全自动编程软件 编辑:程序博客网 时间:2024/04/27 15:01


Product

The problem is to multiply two integers X, Y . (0  X; Y < 10250)
Input
The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.
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

大意:

计算大数据的乘法运算

要点:

使用数组进行存储

注意*0的情况

代码:

#include <iostream>#include <string>using namespace std;string t1, t2;int x[1000], y[1000], ans[1000];void modulo(){for (int i = 0; i < 999; i++){ans[i + 1] += ans[i] / 10;ans[i] = ans[i] % 10;}}void product(int m, int n){for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){ans[j + i] += y[i] * x[j];modulo();}}}int main(){while (cin >> t1 >> t2){int flag = true;int leng1 = t1.length();int leng2 = t2.length();if (leng2 > leng1){string temp = t1;t1 = t2;t2 = temp;}leng1 = t1.length();leng2 = t2.length();for (int i = leng1 - 1, j = 0; i >= 0; i--, j++)x[j] = t1[i] - '0';for (int i = leng2 - 1, j = 0; i >= 0; i--, j++)y[j] = t2[i] - '0';product(leng1, leng2);for (int i = 999; i >= 0; i--){if (ans[i] == 0 && flag)continue;flag = false;cout << ans[i];}if (flag)cout << "0";cout << endl;for (int i = 0; i < 1000; i++){ans[i] = 0;x[i] = 0;y[i] = 0;}}}

0 0
原创粉丝点击