View Code of Problem 3884

#include <iostream>

using namespace std;

void factorial(int n) {
	int a[10001];
	int places, carry, i, j;

	a[0] = 1;
	places = 0;
	for (i = 1; i <= n; i++) {
		carry = 0;
		for (j = 0; j <= places; j++) {
			a[j] = a[j] * i + carry;
			carry = a[j] / 10000;
			a[j] %= 10000;
		}
		if (carry>0) {
			places++;
			a[places] = carry;
		}
	}

	int sum = 0;
	sum += places * 4;
	if (a[places]>999) {
		sum += 4;
	}
	else if (a[places]>99) {
		sum += 3;
	}
	else if (a[places]>9) {
		sum += 2;
	}
	else {
		sum += 1;
	}
	cout << sum << endl;
}

int main(void) {
	int n;
	while (cin >> n) {
		factorial(n);
	}
}

Double click to view unformatted code.


Back to problem 3884