zoukankan      html  css  js  c++  java
  • N!

    题目描述:

    N!

    Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 262144/262144K (Java/Other)
    Total Submission(s) : 68   Accepted Submission(s) : 20

    Font: Times New Roman | Verdana | Georgia

    Font Size: ← →

    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

    这道题要用数组来储存最终的结果。
    注意一个很小的细节(俺在那里测了老半天,伤!),就是0的阶乘为1

    下面附上自己写的代码:
     1 #include <stdio.h>
     2 #include <time.h>
     3 #include <string.h>
     4 
     5 
     6 #define mod 10000
     7 
     8 int main() {
     9     short int rem[10000],i,index,j;
    10     int n,jw;
    11 //    clock_t start,finish;
    12 //    start = clock();
    13 //    double total = 1;
    14     while(~scanf("%d", &n)) {
    15         memset(rem, 0, sizeof(rem));
    16         rem[1] = 1;
    17         index = 2;
    18         jw = 0;
    19 //        if (n == 0)
    20 //            printf("0");
    21         for (i = 1; i <= n;i++) {
    22             for (j=1;j<index;j++) {
    23                 int tem = rem[j]*i+jw;
    24                 rem[j] = tem % mod;
    25                 jw = tem / mod;
    26             }
    27             if (jw) {
    28                 rem[index] = jw;
    29                 jw = 0;
    30                 index++;
    31             }
    32         }
    33         for (i=index-1;i>=1;i--) {
    34             if (i == index-1)
    35                 printf("%d", rem[i]);
    36             else
    37                 printf("%04d", rem[i]); 
    38         }
    39         puts("");
    40     }
    41 //    finish = clock();
    42 //    printf("
    程序用时为:%lf", (double)(finish-start)/CLOCKS_PER_SEC);
    43     return 0;
    44 }
     
  • 相关阅读:
    第4章 栈和队列
    第3章 线性表
    第2章 算法
    第1章 数据结构绪论
    First Blood
    第52条:通过接口引用对象
    第51条:当心字符串连接的性能
    第50条:如果其他类型更合适,则尽量避免使用字符串
    第49条:基本类型优先于装箱基本类型
    第48条:如果需要精确的答案,请避免使用float和double
  • 原文地址:https://www.cnblogs.com/hello-dummy/p/10342488.html
Copyright © 2011-2022 走看看