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

N! HDU 1042

时间:2015-03-12 22:01:53      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

N!

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


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
 
 1 #include <iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<math.h>
 5 #include<stdlib.h>
 6 using namespace std;
 7 int a[8000];
 8 int main()
 9 {
10     int n;
11     int i,j;
12     while(~scanf("%d",&n))
13     {
14         if(n==0)
15         {
16             printf("1\n");
17             continue;
18         }
19         memset(a,0,sizeof(a));
20         a[0]=1;
21         for(i=2;i<=n;i++)
22         {
23             for(j=0;j<8000;j++)
24             a[j]*=i;
25             for(j=0;j<8000;j++)
26             {
27                 a[j+1]+=a[j]/100000;
28                 a[j]%=100000;
29             }
30         }
31         for(i=8000;i>=0;i--)
32         {
33             if(a[i]!=0)
34             break;
35         }
36         printf("%d",a[i--]);
37         for(;i>=0;i--)
38         printf("%05d",a[i]);
39         printf("\n");
40     }
41     return 0;
42 }
43 -----------------------------------------------
44 #include <iostream>
45 #include<stdio.h>
46 #include<string.h>
47 #include<math.h>
48 #include<stdlib.h>
49 using namespace std;
50 int a[100000];
51 int main()
52 {
53     //freopen("in.txt","r",stdin);
54     int n;
55     int i,j,temp;
56     while(~scanf("%d",&n))
57     {
58         memset(a,0,sizeof(a));
59         a[0]=1;
60         int count=1;
61         for(i=1;i<=n;i++)
62         {
63             int k=0;
64             for(j=0;j<count;j++)
65             {
66                 temp = a[j]*i+k;
67                 a[j]=temp%10;
68                 k=temp/10;
69             }
70             while(k)
71             {
72                 a[count++]=k%10;
73                 k/=10;
74             }
75         }
76         for(i=100000;i>=0;i--)
77         {
78             if(a[i]!=0)
79             break;
80         }
81         for(;i>=0;i--)
82         printf("%d",a[i]);
83         printf("\n");
84     }
85     return 0;
86 }

 

 

N! HDU 1042

标签:

原文地址:http://www.cnblogs.com/xuesen1995/p/4333445.html

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