微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NYOJ 28 大数阶乘 HDOJ 1042 N!

大数阶乘

时间限制: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; 
}
 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐