加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

NYOJ 28 大数阶乘 HDOJ 1042 N!

发布时间:2020-12-14 02:27:39 所属栏目:大数据 来源:网络整理
导读:大数阶乘 时间限制: 3000 ms ?|? 内存限制: 65535 KB 难度: 3 描述 我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它? 输入 输入一个整数m(0m=5000) 输出 输出m的阶乘,并在输出结束之后输入一个换行符 样例输入 50

大数阶乘

时间限制:3000 ms ?|? 内存限制:65535 KB
难度:3
描述
我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它?
输入
输入一个整数m(0<m<=5000)
输出
输出m的阶乘,并在输出结束之后输入一个换行符
样例输入
50
样例输出
30414093201713378043612608166064768844377641568960512000000000000


ac代码:

#include<stdio.h>
#include<string.h>
int main()
{
    int s,n,i,j,c;
    while(scanf("%d",&n)!=EOF)
     {
        int a[20000]={0};
        a[0]=1;
        for(i=2;i<=n;i++)
        {
            c=0;
                for(j=0;j<=20000;j++)
                {
                    s=a[j]*i+c;
                    a[j]=s%10;
                    c=s/10;
                }
        }
        for(j=20000;j>=0;j--)
        {
            if(a[j]!=0)
                break;
        }
        for(i=j;i>=0;i--)
        {
           printf("%d",a[i]);
        }     
        printf("n");
       }
       return 0;
}


附上HDOJ的一道题:

HDOJ?? 1042?? N!

N!

Time Limit: 10000/5000 MS (Java/Others)????Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 65135????Accepted Submission(s): 18625


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
ac代码:

#include<stdio.h>  
int main()  
{  
    int n;  
    while(scanf("%d",&n)!=EOF)  
    {  
        int i,a[100200],c,d=1,t,j;  //注意HDOJ这道题n的最大值为10000,NYOJ的为5000,所以要定义足够长的数组
        a[1]=1;  
        for(i=1;i<=n;i++)  
        {  
            for(j=1,c=0;j<=d;j++)  
            {  
                t=a[j]*i+c;  
                a[j]=t%10;  
                c=t/10;  
            }  
            while(c)  
            {  
                a[++d]=c%10;  
                c/=10;  
            }  
        }  
        for(i=d;i>=1;i--)  
            printf("%d",a[i]);  
        printf("n");  
    }  
    return 0; 
}
?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读