hdu 1042 N! 大数用数组模拟
2012-08-19 17:16
411 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1042
[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
[align=left]Author[/align]
JGShining(极光炫影)
思路:用一个一维数组模拟大数,数组的每一个元素表示5位(可以自己设定),每次计算将要计算得数与该数组相乘,然后再处理下进位的问题就行了。
N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 34360 Accepted Submission(s): 9611[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
[align=left]Author[/align]
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; }
相关文章推荐
- HDU 1047(大数相加)(数组模拟)
- HDU 1753 大明A+B(大数加法,数组模拟)
- hdu_1042(模拟大数乘法)
- hdu-1715大菲波数(数组表示大数)
- HDU 2054 A == B ?(Java大数用equal???C模拟也不能错过)
- HDU 1042 大数阶乘
- HDU 5920 Ugly Problem(大数减法 模拟)
- HDU 1042求N! (大数)
- HDU 4576(2013杭州邀请赛) 滚动数组+模拟过程
- HDU1042 N! 大数的阶乘
- N!(1042)hdu(大数阶乘)
- hdu 1042 N!(大数)
- HDU-1042(大数阶乘)
- HDU - 1042 - N!(大数阶乘)
- HDU 1042 N! N的阶乘(大数)
- hdu 1042 N!(大数的阶乘)
- HDU 5818 2016多校赛第七场 数组模拟链表,来模拟栈
- hdu 1042 大数阶乘(优化)
- hdu 1250 大数相加并用数组储存
- Numerically Speaking hdu 1314 大数模拟