1042杭电 N!

Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!

Input
One N in one line, process to the end of file.

Output
For each N, output N! in one line.

Sample Input
   
   
   
   
1 2 3

Sample Output
   
   
   
   
1 2 6
 
#include<stdio.h>
#include<string.h>
int a[40100];
int fun(int n)
{

	int i,j,b;
	memset(a,0,sizeof(a));
	a[1]=1;
	for(i=2;i<=n;++i)
	{
		int k=0;
		for(j=1;j<=40000;++j)
		{
			b=a[j]*i+k;
			k=b/10;
			a[j]=b%10;
		}
	}
	for(i=40000;i>0&&a[i]==0;i--)
	;
	return i;
}
int main()
{
	int n,j;
	while(~scanf("%d",&n))
	{
		for(j=fun(n);j>=1;--j){
			printf("%d",a[j]);
			//printf("%d",fun(n));
		}
		printf("\n");	
	}
	return 0;
}


 
#include<stdio.h>
int a[40010];
int f(int n)
{
    int i,r,j,m;
    for(i=1;i<=40000;i++)
        a[i]=0;
    a[1]=1;
    
    for(i=2;i<=n;i++)
    {
        r=0;
        for(j=1;j<=40000;j++)//存放数的每一位
        {
            m=a[j]*i+r;
            a[j]=m%10;
            r=m/10;
        }
    }
    for(i=40000;i>0&&a[i]==0;i--);
    
    return i;

}
int main()
{
    int n,j,b;
    while(~scanf("%d",&n))
    {
        b=f(n);
        for(j=b;j>=1;j--)
            printf("%d",a[j]);
        printf("\n");

    }

    return 0;
}
 
 
 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=50000;
int main(){
	 
	int N,i,j;
	int a[MAX];
	while(scanf("%d",&N)!=EOF){
		memset(a,0,sizeof(a));
		int digit=1,t=0;a[0]=1;
		for(i=1;i<=N;i++){
			for(j=0;j<=digit;j++){
				t=t+a[j]*i;
				a[j]=t%10;
				t/=10;
				if(t>0&&j+2>digit){
					digit++;
				}
			}
		}
		while(a[digit]==0)digit--;
		for(i=digit;i>=0;i--){
			printf("%d",a[i]);
		}
		puts("");
	}
return 0;
} 


 

你可能感兴趣的:(1042杭电 N!)