您的位置:首页 > 其它

UVa 10106 Product

2014-07-01 08:36 369 查看
高精度乘法问题,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代码:

//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxn = 265;
char a[maxn], b[maxn];
int x[maxn], y[maxn], mul[maxn * 2 + 10];
void Reverse(char s[], int l);

int main(void)
{
#ifdef LOCAL
freopen("10106in.txt", "r", stdin);
#endif

while(gets(a))
{
gets(b);
int la = strlen(a);
int lb = strlen(b);
memset(mul, 0, sizeof(mul));
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
Reverse(a, la);
Reverse(b, lb);
int i, j;
for(i = 0; i < la; ++i)
x[i] = a[i] - '0';
for(i = 0; i < lb; ++i)
y[i] = b[i] - '0';

for(i = 0; i < lb; ++i)
{
int s = 0, c = 0;
for(j = 0; j < maxn; ++j)
{
s = y[i] * x[j] + c + mul[i + j];
mul[i + j] = s % 10;
c = s / 10;
}
}

for(i = maxn * 2 + 9; i >= 0; --i)
if(mul[i] != 0)
break;
if(i < 0)
cout << 0;
else
{
for(; i >=0; --i)
cout << mul[i];
}
cout << endl;
}
return 0;
}
//用来反转数组
void Reverse(char s[], int l)
{
int i;
char t;
for(i = 0; i < l / 2; ++i)
{
t = s[i];
s[i] = s[l - i -1];
s[l - i -1] = t;
}
}


代码君
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: