Problem B: Product
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 51 Solved: 15
[ Submit][ Status][ Web Board]
Description
The problem is to multiply two integers X, Y. (0<=X,Y<10
250)
Input
The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.
Output
For each input pair of lines the output line should consist one integer the product.
Sample Input
12
12
2
222222222222222222222222
12
2
222222222222222222222222
Sample Output
144444444444444444444444444
HINT
解题报告:
1、大数乘法,还行
2、通过一个int数组,存储在每个位置相乘得到的值。
3、通过细节处理,得到大数相乘的值
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define Max 550
int cake[Max];
char c_a[Max],c_b[Max];
char result[Max];
int main() {
while (~scanf("%s %s",c_a,c_b)) {
int count=0,i,j;
memset(cake,0,sizeof(cake));
memset(result,'\0',sizeof(result));
int na = strlen(c_a);
int nb = strlen(c_b);
for (i=na-1; i>=0; i--) {
for (j=nb-1; j>=0; j--) {
cake[i+j+1] = cake[i+j+1] + (c_a[i]-'0') * (c_b[j]-'0');
}
}
for (i=na+nb-1; i>=0; i--) {
cake[i] = cake[i] + count;
count = cake[i] / 10;
cake[i] = cake[i] % 10;
}
for (i=0; !cake[i] && i<na+nb-1; i++) {
}
j=0;
for ( ; i<na+nb; i++) {
result[j++] = cake[i] + '0';
}
printf("%s\n",result);
}
return 0;
}
3746

被折叠的 条评论
为什么被折叠?



