NYOJ 28 大数阶乘 HDOJ 1042 N!

来源:互联网 发布:火并软件 编辑:程序博客网 时间:2024/06/04 01:08

大数阶乘

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

Sample Output
126
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; }



 


0 0
原创粉丝点击