UVa 10106 Product
2014-07-01 08:36
369 查看
高精度乘法问题,WA了两次是因为没有考虑结果为0的情况。
AC代码:
代码君
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; } }
代码君
相关文章推荐
- UVA - 10106 Product 第二种解法
- Product(uva10106)
- UVa 10106 Product
- UVA 10106 - Product
- UVa - 10106 - Product
- UVA 10106 Product (大数相乘)
- uva 10106 Product(高精度乘法)
- UVA - 10106 Product
- UVA 10106 - Product (大数相乘问题)
- uva-10106 - Product
- UVA - 10106 Product
- UVa 10106 - Product
- UVA 10106-Product(大数乘法)
- (高精度运算4.7.21)UVA 10106 Product(大数乘法)
- (高精度运算4.7.21)UVA 10106 Product(大数乘法)
- UVA 10106 - Product
- Uva 10106 Product(use java)
- UVA 10106 Product (高精度乘法)
- UVA - 10106 Product
- UVA 10106 - Product(大数乘法)