码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1042 N!(大数的阶乘)

时间:2014-10-22 20:11:43      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   java   for   strong   

N!

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


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
 
这个题目就是一个大数的乘法,不难,但是半年前的我写这个题目的时候写了很长很长的代码 麻烦的很 花了很久才过的  ,现在想起来就重新写了一遍 代码精简了好多,而且也美观了很多 ,哈哈 事实证明我这半年还是有进步的  O(∩_∩)O!
 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 #define ll long long
 8 
 9 int num[80000];
10 
11 int main()
12 {
13     int n;
14     while(~scanf("%d",&n))
15     {
16         memset(num,0,sizeof(num));
17         num[0]=1;
18         int pre=0;
19         int len=1;
20         for(int i=2;i<=n;i++)
21         {
22             pre=0;
23             for(int j=0;j<len;j++)
24             {
25                 num[j]=num[j]*i+pre/10;
26                 pre=num[j];
27                 num[j]%=10;
28             }
29             while(pre>9)
30             {
31                 num[len]=pre/10%10;
32                 len++;
33                 pre/=10;
34             }
35         }
36         for(int i=len-1;i>=0;i--)
37         printf("%d",num[i]);
38         printf("\n");
39     }
40     return 0;
41 }

 

hdu 1042 N!(大数的阶乘)

标签:des   style   blog   color   io   os   java   for   strong   

原文地址:http://www.cnblogs.com/mis-xiao/p/4043922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!