• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
若忆_star
博客园    首页    新随笔    联系   管理    订阅  订阅
hdu1042 N!

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042

N!

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


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
 
题目大意:求N!        比如求123!我们肯定是要1*2*3*.....*123;如果直接这样做下去,毋庸置疑的会超时,那么我们就要将他一位一位存下来。一位一位的乘过去。做过的东西,还要想好久,还是不扎实,一定要多动脑。
 
详见代码。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 int num[80000];
 7 int main ()
 8 {
 9     int n,pre;
10     while (~scanf("%d",&n))
11     {
12         memset(num,0,sizeof(num));
13         num[0]=1;
14         int len=1;
15         for (int i=2; i<=n; i++)
16         {
17             pre=0;
18             for (int j=0; j<len; j++)
19             {
20                 num[j]=num[j]*i+pre/10;
21                 pre=num[j];
22                 num[j]=num[j]%10;
23             }
24             while (pre>9)
25             {
26                 num[len]=pre/10%10;
27                 len++;
28                 pre/=10;
29             }
30         }
31         for (int i=len-1; i>=0; i--)
32             printf ("%d",num[i]);
33         printf ("\n");
34     }
35     return 0;
36 }

 

 

 

posted on 2014-12-15 23:24  若忆_star  阅读(189)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3