天天看點

杭電HDOJ 1042 解題報告 N!

N!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 41380    Accepted Submission(s): 11478

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
        

Author JGShining(極光炫影)

基礎題。(大數處理)

解題思路:

For循環、數組處理大數

注意:0!=1;

代碼:(剛開始刷題時寫的比較濫、勿噴)

#include<stdio.h>
#include<string.h>
const int maxn=40000;
int main()
{
    int i,j,n;
    int f[40000];
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0){
            printf("1\n");
            continue;
        }
    memset(f,0,sizeof(f));
    f[0]=1;
    for(i=2;i<=n;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;
}