hdu 1042 N! 高精度运算
HDU 运算 高精度
2023-09-27 14:22:40 时间
N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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
题目分析:用数组模拟乘法。让a[0]保存结果的各位,a[1]是十位,a[2]是百位……(为什么要逆序表示呢?因为如果按照从高到低的顺序储存,一旦进位的话就……),则每次只需要模拟手算即可完成n!。在输出时需要忽略前导0.注意,如果结果本身就是0,那么忽略前导0后将什么都不输出。所幸n!肯定不等于0,因此本题可以忽略这个细节。
#include<stdio.h> #include<string.h> const int maxn=40000; /*数组不能太小,小了存不下*/ int a[maxn]; int main() { int i,j,n; while(~scanf("%d",&n)) { memset(a,0,sizeof(a)); a[0]=1; for(i=2;i<=n;i++) { int c=0; /*保存进位*/ for(j=0;j<maxn;j++) { int s=a[j]*i+c; a[j]=s%10; c=s/10; } } for(j=maxn-1;j>=0;j--) /*去掉前导零*/ if(a[j]) break; for(i=j;i>=0;i--) printf("%d",a[i]); printf("\n"); } return 0; }
相关文章
- hdu 2014鞍山赛区 5073 Galaxy
- HDU 3820 Golden Eggs (SAP | Dinic)
- hdu 2851(最短路)
- hdu 1083 Courses
- HDU 3461 Code Lock(并查集的应用+高速幂)
- hdu 2066 一个人的旅行(dijkstra)
- HDU 4118 树形DP Holiday's Accommodation
- HDU 4604 Deque 二分最长上升子序列
- hdu 1253 胜利大逃亡(BFS)
- HDU 2084 数塔
- Keywords Search HDU - 2222 AC自动机板子题
- Ancient Printer HDU - 3460 贪心+字典树
- HDU 4685 Prince and Princess (2013多校8 1010题 二分匹配+强连通)
- HDU 3966 Aragorn's Story (树链剖分+树状数组)
- HDU 1079 Calendar Game(简单博弈)
- HDU 3072 Intelligence System (强连通分量)
- Hdu 2473(并查集删除操作) Junk-Mail Filter
- HDU 3103 Shoring Up the Levees(计算几何 搜寻区域)
- HDU 4652 Dice (概率DP)