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): 34360 Accepted Submission(s): 9611
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
Author
JGShining(极光炫影)
思路:用一个一维数组模拟大数,数组的每一个元素表示5位(可以自己设定),每次计算将要计算得数与该数组相乘,然后再处理下进位的问题就行了。
#include <stdio.h> #include <string.h> int num[8000]; //代表了最终结果的最大长度为8000*5(num中每位定义的位数) int main() { int n; int i,j,t,y; while(scanf("%d",&n)==1) { if(n==0) { printf("1\n"); continue; } memset(num,0,sizeof(num)); num[1]=1; t=1; for(i=2;i<=n;i++) //核心部分,num数组的所有位数表示当前计算出的结果 { y=0; for(j=1;j<=t;j++) { num[j]=num[j]*i+y; y=num[j]/100000; num[j]%=100000; } while(y) //有余进位 { num[++t]=y%100000; y/=100000; } } printf("%d",num[t]); for(i=t-1;i>=1;i--) printf("%05d",num[i]); //格式化输出,每一个num代表了结果中的5位数 printf("\n"); } return 0; }