AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 1. Elementary Problem Solving :: Big Number
// 10106 - Product
#include <iostream>
#include <cstring>
//#include <cstdio>
#define MAXN 1000
using namespace std;
char A[MAXN], B[MAXN];
int a[MAXN], b[MAXN], c[MAXN];
int main(void)
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int lenA, lenB, carry, index, t, i, j, k, n;
while(cin >> A >> B)
{
lenA = strlen(A);
lenB = strlen(B);
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
for(i=lenA-1, k=0; i>=0; i--, k++)
a[k] = A[i] - '0';
for(i=lenB-1, k=0; i>=0; i--, k++)
b[k] = B[i] - '0';
index = 0;
for(i=0; i<=lenA; i++)
{
carry = 0;
t = index;
for(j=0; j<=lenB; j++)
{
carry += a[i] * b[j];
c[t] += carry % 10;
carry /= 10;
if(c[t] > 9) // !!!
{
carry += c[t] / 10;
c[t] %= 10;
}
t++;
}
index++;
}
n = MAXN-1;
while(n>0 && !c[n]) // 特别注意结果为零的情况!!!
n--; // 数组不能越界到-1!!!
for(i=n; i>=0; i--) // 同时必须输出一个零!!!
cout << c[i];
cout << endl;
}
return 0;
}