UVa - 10106 - Product
2013-08-08 14:50
357 查看
题目大意:
求两个数的乘积,用高精度写。但要注意结果为0 的情况!
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 1010
using namespace std;
char str1
, str2
;
int ans
;
int main()
{
int i, j;
while (scanf("%s", str1) != EOF)
{
memset(ans, 0, sizeof(ans));
reverse(str1, str1 + strlen(str1));
scanf("%s", str2);
reverse(str2, str2 + strlen(str2));
for (i = 0; i < strlen(str1); i ++)
{
for (j = 0; j < strlen(str2); j ++)
{
ans[i + j] += (str1[i] - '0') * (str2[j] - '0');
if (ans[i + j] > 9)
{
ans[i + j + 1] += ans[i + j] / 10;
ans[i + j] %= 10;
}
}
}
for (j = 1009; j >= 0; j --)
{
if (ans[j] != 0)
{
break;
}
if (j == 0)
{
printf("0");
}
}
for (i = j; i >= 0; i --)
{
printf("%d", ans[i]);
}
printf("\n");
}
return 0;
}
The problem is to multiply two integers X, Y. (0<=X,Y<10250)
The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.
For each input pair of lines the output line should consist one integer the product.
求两个数的乘积,用高精度写。但要注意结果为0 的情况!
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 1010
using namespace std;
char str1
, str2
;
int ans
;
int main()
{
int i, j;
while (scanf("%s", str1) != EOF)
{
memset(ans, 0, sizeof(ans));
reverse(str1, str1 + strlen(str1));
scanf("%s", str2);
reverse(str2, str2 + strlen(str2));
for (i = 0; i < strlen(str1); i ++)
{
for (j = 0; j < strlen(str2); j ++)
{
ans[i + j] += (str1[i] - '0') * (str2[j] - '0');
if (ans[i + j] > 9)
{
ans[i + j + 1] += ans[i + j] / 10;
ans[i + j] %= 10;
}
}
}
for (j = 1009; j >= 0; j --)
{
if (ans[j] != 0)
{
break;
}
if (j == 0)
{
printf("0");
}
}
for (i = j; i >= 0; i --)
{
printf("%d", ans[i]);
}
printf("\n");
}
return 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
相关文章推荐
- UVa 10106 - Product
- UVa 10106 - Product
- UVA 10106 - Product
- uva 424 Integer Inquiry(高精度加法模板) 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
- UVa 10106 - Product
- UVA - 10106 Product 第二种解法
- uva 10106 Product
- UVA 10106 Product (高精度乘法)
- UVA 10106 Product (大数相乘)
- Product(uva10106)