Hi everyone,
I have later discovered my mistake. My mistake is that in adding the subtotal, I FORGOT TO CARRY OVER WHEN A DIGIT IS MORE THAN 10. And now I got this problem Accepted
For those of you having trouble with this problem, here is my Accepted source code.
[cpp]#include <stdio.h>
#include <string.h>
char n1[503], n2[503], product[503];
void getdigits(char s1[252], char s2[252]) {
int i, j;
memset(n1, 0, sizeof(n1));
memset(n2, 0, sizeof(n2));
for (i = strlen(s1) - 1, j = 501; i >= 0; i--, j--)
n1[j] = s1[i] - '0';
for (i = strlen(s2) - 1, j = 501; i >= 0; i--, j--)
n2[j] = s2[i] - '0';
}
void multiply() {
char subtotal[503];
int i, j, k;
memset(product, 0, sizeof(product));
for (i = 501; i >= 251; i--) {
memset(subtotal, 0, sizeof(subtotal));
for (j = 501, k = i; j >= 251; j--, k--) {
subtotal[k] += n1[i] * n2[j];
subtotal[k - 1] += subtotal[k] / 10;
subtotal[k] %= 10;
}
// add subtotal
for (j = 501; j > 0; j--) {
product[j] += subtotal[j];
product[j - 1] += product[j] / 10;
product[j] %= 10;
}
}
}
int main() {
bool lead;
char str1[252], str2[252];
int i;
while (scanf("%s%s", &str1, &str2) == 2) {
getdigits(str1, str2);
multiply();
lead = true;
for (i = 0; i < 502; i++) {
if (lead && product[i] > 0)
lead = false;
if (!lead)
printf("%d", product[i]);
}
if (lead)
printf("0");
printf("\n");
}
return 0;
}[/cpp]