HDU1042-N!(高精度算法)

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的阶乘的值,n最大可达10000!

解析:
一看就是高精度的算法题,开始我打算打表,结果提交后空间超了!于是就不打表了,可是提交后还是WA,后来发现0的阶乘的值是1而不是0(想不通)!

代码:

#include<iostream>
#include<cstdio>

using namespace std;

int list[500005];

int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		list[0]=list[1]=1;
		for(int i=2;i<=n;i++)
		{
			int len=list[0],v=0;
			for(int j=1;j<=len;j++)
			{
				v=v+list[j]*i;
				list[j]=v%10;
				v/=10;
			}
			while(v)
			{
				list[++len]=v%10;
				v/=10;
			}
			list[0]=len;
		}
		for(int i=list[0];i>0;i--)
		{
			printf("%d",list[i]);
			list[i]=0;
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43357583/article/details/105983805