阶乘模版

#include<cstdio>
#include<cstring>
#include <iostream>
using namespace std;
#pragma warning(disable : 4996)
#define maxn 50000
int f[maxn];
int main()
{
	int m, i, j;
	while(scanf("%d", &m) != EOF)
	{
		memset(f, 0, sizeof(f));
		f[0] = 1;
		for(i = 2; i <= m; i++)
		{
			int c = 0;
			for(j = 0; j < maxn; j++)
			{
				int s = f[j] * i + c;
				f[j] = s % 10;
				c = s / 10;
			}
		}
		for(j = maxn - 1; j >= 0; j--)
		{
			if(f[j])
			{
				break;
			}
		}
		for(i = j; i >= 0; i--)
		{
			printf("%d", f[i]);
		}
		printf("\n");
	}
	return 0;
}


压位:

#include<cstdio>
#include<cstring>
#include <iostream>
using namespace std;
#pragma warning(disable : 4996)

void factorial(int n)
{
	long long c, a[10000];
	int i, j, m = 0; 
	a[0] = 1;
	for(i = 1; i <= n; i++)
	{ 
		c = 0; 
		for(j = 0; j <= m; j++)
		{ 
			a[j] = a[j] * i + c; 
			c = a[j] / 100000;
			a[j] = a[j] % 100000;
		} 
		if(c > 0)
		{
			m++;
			a[m] = c;
		} 
	}
	printf("%lld", a[m]); 
	for(i = m - 1; i >= 0; i--)
	{
		printf("%5.5lld", a[i]);
	}
	printf("\n");
} 
int main()
{
	int m;
	while(scanf("%d", &m) != EOF)
	{
		factorial(m);
	}
	return 0;
}



posted @ 2013-06-07 11:04  N3verL4nd  阅读(148)  评论(0编辑  收藏  举报