题目链接: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 }