2018.11.11
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
const int MAXN = 3000;
int res[MAXN];
void print_res()
{
int pos;
for (pos = MAXN - 1; !res[pos]; pos--); // omit pos >= 0
for (int i = pos; i >= 0; --i)
{
printf("%d", res[i]);
}
putchar('\n');
}
int main()
{
// n! = ?
// freopen("input.txt", "r", stdin);
int n;
while (scanf("%d", &n) == 1)
{
memset(res, 0, sizeof(res));
res[0] = 1;
for (int mul = 1; mul <= n; ++mul)
{
int carry = 0;
for (int i = 0; i < MAXN; ++i)
{
int tmp = res[i] * mul + carry;
res[i] = tmp % 10;
carry = tmp / 10;
}
// print_res();
}
printf("%s:", "final result: ");
print_res();
}
}
2018.9
#include<stdio.h>
#include<string.h>
const int size1 = 3000;
const int size2 = 5;
int res[size1]; //both 1000! at max
int mul1[size1];
int mul2[size2]; //1000 at max
int main()
{
//pay attention to initialization of array
//and array's boundary
int n; //n!
while(scanf("%d", &n) == 1)
{
bool first = true; //set mul1 = 1
for(int i = 1; i <= n; ++i)
{
//
//printf("now is %d\n", i);
if(first)
{
memset(mul1, 0, sizeof(mul1));
mul1[0] = 1;
first = false;
}
else
{
memcpy(mul1, res, sizeof(mul1));
}
memset(res, 0, sizeof(res));
memset(mul2, 0, sizeof(mul2));
for(int k = 0, cur = i; cur > 0; ++k, cur /= 10)
{
mul2[k] = cur % 10;
}
for(int pos = 0; pos < size1; ++pos)
{
//filling in every digit in res
for(int j = 0; j <= pos && j < size2; ++j)
{
res[pos] += mul1[pos-j] * mul2[j];
//if(mul1[pos-j] * mul2[j])
//printf("mul1 mul2 (%d, %d): %d %d\n", pos-j, j, mul1[pos-j], mul2[j]);
}
res[pos + 1] += res[pos] / 10;
res[pos] %= 10;
//if(res[pos+1] || res[pos])
// printf("res.pos+1 res.pos (%d, %d): %d %d\n", pos+1, pos, res[pos+1], res[pos]);
}
}
printf("%d! is :", n);
int start;
for(start = size1 - 1; !res[start] && start >= 0; --start);
for(int i = start; i >= 0; --i)
{
printf("%d", res[i]);
}
putchar('\n');
}
}