【ACM】hdu_1042_N!_201308071639
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
//此解法节省时间
最新文章
- Johnson 全源最短路径算法
- SDK接入(3)之iOS内支付(In-App Purchase)接入
- iOS8沙盒路径的变化
- [asp.net]c# winform打印类
- Python-Tkinter几何布局管理(转)
- Java帮助文档的生成
- hdu 4744 最小费用流
- QNX---- interrupts 例程
- 数据结构中常见的树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)
- 轻松理解 Spark 的 aggregate 方法
- Java中构造方法、实例方法、类方法的区别
- Cheapest Palindrome [POJ3280] [区间DP] [经典]
- redis学习笔记(面试题)
- jquery----数据增删改
- SpringBoot------如何将项目打成war包
- mysql的事务,隔离级别和锁
- Python : locals and globals
- 使用MonkeyTest对Android客户端进行压力测试
- The role of the inter-controller consensus in the placement of distributed SDN controllers
- learning uboot fstype command
热门文章
- oc33--构造方法2
- CH Round #24 - 三体杯 Round #1-C 逃不掉的路
- GStreamer系列 - 基本介绍
- IP地址库
- python--5、包
- 远程连接Oracle设置
- 各个数据库中,查询前n条记录的方法
- 复习java基础第七天(反射)
- 【sqli-labs】 less27 GET- Error based -All you Union&;Select Belong to us -String -Single quote(GET型基于错误的去除了Union和Select的单引号字符型注入)
- 【sqli-labs】 less25 GET- Error based -All you OR&;AND belong to us -string single quote(GET型基于错误的去除了or和and的单引号注入)