杭电HDOJ 1042 解题报告

来源:互联网 发布:影视美术设计考研知乎 编辑:程序博客网 时间:2024/06/10 14:55

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
123
 

Sample Output
126
 

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;}