首页 > 其他 > 详细

N!(大数阶乘)

时间:2020-03-30 18:35:33      阅读:95      评论:0      收藏:0      [点我收藏+]

N!

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 105341 Accepted Submission(s): 31438

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>
#define N 40001
int main()
{
    int n;
    int ans[N];
    
    int i,j,up;        // up 记录进位
    int temp ;        
    while(~scanf("%d",&n))
    {
        memset(ans,0,sizeof(ans));   //把数组全置零
        ans[0]=1;
        for(i = 2; i <= n; i++)
        {
            up=0;
            for(j=0;j<N;j++)
            {
                temp=ans[j]*i+up;
                ans[j]=temp % 10;
                up=temp/10;
            }
            
        }
        for(i=N-1;i>=0;i--) if(ans[i]) break;   // 倒置输出 过滤前面的 0
        for( ; i>=0; --i)
            printf("%d",ans[i]);
        printf("\n");
    }
    return 0;
}

N!(大数阶乘)

原文:https://www.cnblogs.com/lkfsblogs/p/12600206.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!