微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

求大数阶乘的位数ACM Big Number问题

哎~~~开始A题玩玩,ACM还是蛮有味么~~

问题描述:求一个大数阶乘的位数

解决策略:

方法一:log10(n!)=log10(1*2*3…*n)=log10(1)+log10(2)+…+log10(n)+1

即对log10(n!)的值取整加1就是n!的位数——朴素方法

//Author:YunMengZe
//DateTime:2013.10.30
//Description:求给定数阶乘的位数
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int n;
	double d=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		d+=log10(double(i));
	}
	cout<<"n阶乘的位数位: "<<(int)(d+1)<<endl;
	return 0;
}


 

方法二:近似计算的方法斯特林公式。n!近似等于sqrt(2*pi*n) * (n/e)^n

//Author:YunMengZe
//DateTime:20131030
//Method:斯特林公式 n!=sqrt(2*pi*n)*(n/e)^n
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.1415926
#define e  2.71828
int main()
{
 int n;
 double d=0;
 cin>>n;
 if(n==1||n==0)
  d=1;
 else
 {
  d=log10(double(2*PI*n))/2 + n*log10(double(n/e));
  d=ceil(d); 
 }
 cout<<"n阶乘的位数为:"<<d<<endl;
 return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐