HDU 1042 大数据、高精度,求n的阶乘

时间:2022-10-03 03:37:55

本人目前纯属菜鸟,寒假略自学了数据结构,这是我在CSDN发的第一篇博文
HDU 1042 题目及代码

N!

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


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
 
几点总结:

        之前接触过几次高精度,比如高精度的乘法,刚开始运用了两个大数相乘的方法,将两个乘数都放到数组,无论如何都是超时超内存,无奈之下只能百度了一下下,才发现这题的乘数是有大小限制的,只有结果是大数需要用数组临时保存,不动脑子的后果啊,明白这个后难度不大,代码仅供参考。。。ps:第一篇博文,不喜勿喷。。。

#include<iostream>
using namespace std;
#define MAX 10000
int a[MAX*4];
void fun(int n)
{
int i,j,temp;
a[1]=1;
a[0]=1;
for(i=2;i<=n;i++)
{
temp=0;
for(j=1;j<=a[0];j++)
{
a[j]*=i;
a[j]+=temp;
temp=a[j]/10;
a[j]%=10;
}
a[0]=j-1;
while(temp)
{
a[j]=temp%10;
temp/=10;
a[0]=j;
j++;
}
}
}
void main()
{
int n,i;
while(cin>>n)
{
memset(a,0,sizeof(a));
fun(n);
if(n==1)
{
cout<<1<<endl;
continue;
}
for(i=a[0];i>=1;i--)
cout<<a[i];
cout<<endl;
}
}

HDU 1042 大数据、高精度,求n的阶乘