NYOJ-28-大数阶乘

大数阶乘

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 3
描述
我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它?
输入
输入一个整数m(0<m<=5000)
输出
输出m的阶乘,并在输出结束之后输入一个换行符
样例输入
50
样例输出
30414093201713378043612608166064768844377641568960512000000000000                                               

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>

using namespace std;

int a[1000000];//用来储存每一位得到的数
int main()
{
	int temp,digit,n;//temp每次的得数 digit每次得到的位数
	scanf("%d",&n);
	a[0] = 1;//从1开始乘
	digit = 1;//位数从第一位开始 
	for(int i = 2; i <= n; i++)
	{
		int num = 0;//进位得到的数
		for(int j = 0; j < digit; j++)
		{
			temp = a[j]*i + num;//将每一位都分别乘于i,并且加上进位数 
			a[j] = temp%10;//将一个数的每一位利用数组进行保存 
			num = temp/10;//获得进位数 
		}	
		while(num)//若存在进位 
		{
			a[digit] = num%10;//继续保存
			num = num/10;
			digit++;//累计位数 
		} 
	} 
	for(int i = digit-1; i >= 0; i--)//倒序输出每一位 
		printf("%d",a[i]);
	printf("\n");			
	 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/cutedumpling/article/details/80159835