您的位置:首页 > 其它

hdu1042 N!

2015-10-12 14:22 323 查看
题目链接: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


[align=left]Problem Description[/align]
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
 
 

[align=left]Input[/align]
One N in one line, process to the end of file.
 
 

[align=left]Output[/align]
For each N, output N! in one line.
 
 

[align=left]Sample Input[/align]

1
2
3

 
 

[align=left]Sample Output[/align]

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 }


 

 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: