cpptemplate.cpp
来自「阶乘的C++实现方法」· C++ 代码 · 共 48 行
CPP
48 行
//=====================================
// title: 数组排序
// author: cjj
// date: 2007-10-09
/* Description:
*/
//=====================================
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int Factorial(int n);
double Stirling(int n); //斯特林近似法(stirling'sapproximation)
const double PI=3.1415926;
const double E=2.718;
int main()
{
for(int n; cin>>n;)
{
cout<<fixed<<setprecision(2)<<setw(6)<<Stirling(n)<<endl;
cout<<setw(6)<<Factorial(n)<<endl;
}
return 0;
}
double Stirling(int n)
{
return sqrt(2*PI*n)*pow(n/E,n);
}
int Factorial(int n)
{
int result=1;
if(n==0) return 1;
for(int i=1; i<=n; i++)
{
result*=i;
}
return result;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?