您的位置:首页 > 其它

uva 10106 Product(高精度乘法)

2013-07-25 20:59 471 查看


 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

题目大意:两个数求积。
解题思路:高精度乘法。

#include<iostream>
#include<string.h>
using namespace std;

#define N 62505

struct bign{
int len, s
;

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
, str2
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: