uva 10106 Product(高精度乘法)

来源:互联网 发布:淘宝店铺怎样快速升钻 编辑:程序博客网 时间:2024/05/29 07:49

 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
题目大意:两个数求积。

解题思路:高精度乘法。

#include<iostream>#include<string.h>using namespace std;#define N 62505struct bign{int len, s[N];    bign(){memset(s, 0, sizeof(s));len = 1;}    bign operator = (const char *num){len = strlen(num);for (int i = 0; i < len; i++)s[i] = num[len - i - 1] - '0';return *this;}    bign operator * (const bign &c){bign sum;sum.len = 0;        for (int i = 0; i < len; i++){int g = 0;for (int j = 0; j < c.len; j++){int x = s[i] * c.s[j] + g + sum.s[i + j];sum.s[i + j] = x % 10;g = x / 10;}sum.len = i + c.len;while (g){sum.s[sum.len++] = g % 10;g = g / 10;}}return sum;}};int main(){char str1[N], str2[N];while (cin >> str1 >> str2){bign num1, num2, sum;num1 = str1;num2 = str2;sum = num1 * num2;int bo = 0;for (int i = sum.len - 1; i >= 0; i--){if (sum.s[i] || bo){bo = 1;cout << sum.s[i];}}if (bo == 0)cout << bo;cout << endl;memset(str1, 0, sizeof(str1));memset(str2, 0, sizeof(str2));}return 0;}

原创粉丝点击