N!
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 43937    Accepted Submission(s): 12362

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

#include <stdio.h>
#include <string.h>
#define MAX_LEN 40000
int s[MAX_LEN];

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int i,j,t,sum;
        memset(s,0,sizeof(s));
        s[0]=1;
        for(i=2;i<=n;i++)
        {
            t=0;;
            for(j=0;j<MAX_LEN;j++)
            {
                sum=s[j]*i+t;
                s[j]=sum%10;
                t=sum/10;
            }
        }
        for(i=MAX_LEN-1;(i>0)&&(s[i]==0);i--);
        for(;i>=0;i--)
        printf("%d",s[i]);
        printf("\n");
    }
    return 0;   
}

//此解法耗时3500MS

#include <stdio.h>
#include <string.h>
#define MAX_LEN 40000
int a[MAX_LEN];
int main()
{
    int m;
    while(scanf("%d",&m)!=EOF)
    {
        int i,j,len=0,tem,jin;
        memset(a,0,sizeof(a));
        a[0]=1;
        for(i=2;i<=m;i++)
        {
            jin=0;
            for(j=0;j<=len;j++)
            {
                tem=a[j]*i+jin;
                a[j]=tem%10;
                jin=tem/10;
                if(j==len&&jin!=0)  //如果需要进一位,len+1,这样可以减少循环次数
                len++;
                }
            }
            for(i=len;i>=0;i--)
            printf("%d",a[i]);
            printf("\n");
        }
        return 0;
}

//此解法耗时1218MS

//此解法节省时间

最新文章

  1. Johnson 全源最短路径算法
  2. SDK接入(3)之iOS内支付(In-App Purchase)接入
  3. iOS8沙盒路径的变化
  4. [asp.net]c# winform打印类
  5. Python-Tkinter几何布局管理(转)
  6. Java帮助文档的生成
  7. hdu 4744 最小费用流
  8. QNX---- interrupts 例程
  9. 数据结构中常见的树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)
  10. 轻松理解 Spark 的 aggregate 方法
  11. Java中构造方法、实例方法、类方法的区别
  12. Cheapest Palindrome [POJ3280] [区间DP] [经典]
  13. redis学习笔记(面试题)
  14. jquery----数据增删改
  15. SpringBoot------如何将项目打成war包
  16. mysql的事务,隔离级别和锁
  17. Python : locals and globals
  18. 使用MonkeyTest对Android客户端进行压力测试
  19. The role of the inter-controller consensus in the placement of distributed SDN controllers
  20. learning uboot fstype command

热门文章

  1. oc33--构造方法2
  2. CH Round #24 - 三体杯 Round #1-C 逃不掉的路
  3. GStreamer系列 - 基本介绍
  4. IP地址库
  5. python--5、包
  6. 远程连接Oracle设置
  7. 各个数据库中,查询前n条记录的方法
  8. 复习java基础第七天(反射)
  9. 【sqli-labs】 less27 GET- Error based -All you Union&amp;Select Belong to us -String -Single quote(GET型基于错误的去除了Union和Select的单引号字符型注入)
  10. 【sqli-labs】 less25 GET- Error based -All you OR&amp;AND belong to us -string single quote(GET型基于错误的去除了or和and的单引号注入)