• HDU1042 N!


    N!

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 34699    Accepted Submission(s): 9717


    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
     1 /*
     2 #include<stdio.h>
     3 #include<string.h>
     4 #define MAX 60000
     5 int main()
     6 {
     7     int n,i,j,p,t;
     8     int a[MAX];
     9     while(scanf("%d",&n)!=EOF)
    10     {
    11         memset(a,0,sizeof(a));
    12         a[0]=1;//注意赋初值为1
    13         for(i=1;i<=n;++i)
    14         {
    15             t=0;
    16             for(j=0;j<MAX;++j)
    17             {
    18                 p=a[j]*i+t;
    19                 a[j]=p%10;
    20                 t=p/10;
    21             }
    22         }
    23         j=MAX-1;
    24         while(!a[j])
    25         --j;
    26         for(;j>=0;--j)
    27             printf("%d",a[j]);
    28         printf("\n");
    29     }
    30         return 0;
    31 }
    32 //上边代码超时
    33 */
    34 
    35 
    36 #include<stdio.h>
    37 #define MOD 100000//5位一存
    38 int main()
    39 {
    40     int a[8000];//10000!大概有10000*4=40000位,5位一存,数组大小需要开40000/5=8000的大小    
    41     int n,i,j,bit,t;
    42     while(scanf("%d",&n)!=EOF)
    43     {
    44         if(n==0||n==1)
    45         {
    46             printf("1\n");
    47             continue;
    48         }//特殊情况
    49         bit=a[0]=1;
    50         t=0;
    51         for(i=2;i<=n;++i)
    52         {
    53             for(j=0;j<bit;++j)
    54             {
    55                 t+=i*a[j];
    56                 a[j]=t%MOD;
    57                 t=t/MOD;
    58             }
    59             if(t>0)//一轮乘法过后,是否产生新的进位 
    60             {
    61                 a[bit++]=t;
    62                 t=0;
    63             }
    64         }
    65         printf("%d",a[bit-1]);//首位不能为0,所以先输出
    66         for(i=bit-2;i>=0;--i)
    67             printf("%05d",a[i]);//注意:5位一存输出格式05不能写成5
    68 //输出占位符,就这题目来说,输出a[i]时,输出五位,右对齐,不足五位用0补位。例如a[i]=1时,输出00001。
    69         printf("\n");
    70     }
    71     return 0;
    72 }
    功不成,身已退
  • 相关阅读:
    AGC015E Mr.Aoki Incubator
    luogu P3520 [POI2011]SMI-Garbage
    442.Find All Duplicates in an Array
    SICP_2.61-2.62
    sicp_2.59-2.60
    SICP_2.58
    SICP_2.56-2.57
    SICP_2.53-2.55
    SICP_2.52-2.53
    SICP_2.50-2.51
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2660862.html
Copyright © 2020-2023  润新知