//name:周恩冉
//id:89013900
//hw:(2)
#include <iostream>
#define SIZE 500

void factorial(int n)
{
	int i, j, top = 1;
	int num[SIZE] = {1};

	cout << n << "! = "; 
	/* 跟據定義 0! = 1 , 又 1! = 1 是廢話 */
	if(n == 0 || n == 1){
		cout << 1 << endl;
		return;
	}

	/* 從 2 ~ n 每個數都乘一次 */
	for(i = 2;i <= n;i++)
	{
		/* 每一位都乘 */
		for(j = 0;j < top;j++) num[j] *= i;

		/* 處理進位 */
		for(j = 0;j < top;j++)
		{
			if(num[j] > 9){
				num[j + 1] += num[j] / 10;
				num[j] %= 10;
			}
		}

		/* 這裡順便解釋剛才未說明的 top 變數, top 是最高位的前一格 */
		/* 因為只有在最高位才有可能造成長度改變, 所以分開處理 */
		for(;num[top];top++)
		{
			if(num[top] > 9){
				num[top + 1] += num[top] / 10;
				num[top] %= 10;
			}
		}
	}

	/* 不用說, 印出答案 */
	for(i = top - 1;i >= 0;i--) cout << num[i];
	cout << endl;
}

void getdata(int &n)
{
	cout << "你想算多少的階乘(目前定義 500 位, 約可算小於 250 的階乘) : ";
	cin >> n;
}

int main()
{
	int n;
	getdata(n);
	factorial(n);
}

