HDU1012 Calculate e
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1012
解法一:最直观的方法,就是递归计算来求各项之和
#include <iostream>
#include <iomanip>
using namespace std;
double curItem(int n)
{//当前子项
if (n==0)
{
return 1;
}
else
return curItem(n-1)/n;
}
double sum(int n)
{
if(n==0)
return 1;
else
return sum(n-1)+curItem(n);
}
void caculateE(int n)
{//输出指定n下e的值
double tmp1,tmp2=1.0f,result=0.0f;
int i;
result = sum(n);
if(n==0||n==1)
{
cout<<n<<" "<<static_cast<int>(result)<<endl;
}
else if(n==2)
{
cout<<setiosflags(ios::fixed)<<setprecision(1);
cout<<n<<" "<<result<<endl;
}
else
{
cout<<setiosflags(ios::fixed)<<setprecision(9);
cout<<n<<" "<<result<<endl;
}
}
int main(int argc, char *argv[])
{
int n,i;
cout<<"n e"<<endl;
cout<<"- -----------"<<endl;
for(i=0;i<=9;++i)
{
caculateE(i);
}
cin>>i;
return 0;
}
#include <iomanip>
using namespace std;
double curItem(int n)
{//当前子项
if (n==0)
{
return 1;
}
else
return curItem(n-1)/n;
}
double sum(int n)
{
if(n==0)
return 1;
else
return sum(n-1)+curItem(n);
}
void caculateE(int n)
{//输出指定n下e的值
double tmp1,tmp2=1.0f,result=0.0f;
int i;
result = sum(n);
if(n==0||n==1)
{
cout<<n<<" "<<static_cast<int>(result)<<endl;
}
else if(n==2)
{
cout<<setiosflags(ios::fixed)<<setprecision(1);
cout<<n<<" "<<result<<endl;
}
else
{
cout<<setiosflags(ios::fixed)<<setprecision(9);
cout<<n<<" "<<result<<endl;
}
}
int main(int argc, char *argv[])
{
int n,i;
cout<<"n e"<<endl;
cout<<"- -----------"<<endl;
for(i=0;i<=9;++i)
{
caculateE(i);
}
cin>>i;
return 0;
}
解法二:
e=(1+(1+1/2(1+1/3(1+1/4(1+…1/(n-1)(1+1/n)))))
#include <iostream>
#include <iomanip>
using namespace std;
double doCaculate(int n)
{//实际的计算
double tmp1,tmp2=1.0f,result=0.0f;
int i;
for(i=n;i>=1;--i)
{
tmp1 = static_cast<double>(1)/static_cast<double>(i);
tmp2 = 1.0f+tmp1*tmp2;
}
return tmp2;
}
void caculateE(int n)
{//输出指定n下e的值,主要是输出格式的处理
double result = 0.0f;
result = doCaculate(n);
if(n==0||n==1)
{
cout<<n<<" "<<static_cast<int>(result)<<endl;
}
else if(n==2)
{
cout<<setiosflags(ios::fixed)<<setprecision(1);
cout<<n<<" "<<result<<endl;
}
else
{
cout<<setiosflags(ios::fixed)<<setprecision(9);
cout<<n<<" "<<result<<endl;
}
}
int main(int argc, char *argv[])
{
int n,i;
cout<<"n e"<<endl;
cout<<"- -----------"<<endl;
for(i=0;i<=9;++i)
{
caculateE(i);
}
return 0;
}
#include <iomanip>
using namespace std;
double doCaculate(int n)
{//实际的计算
double tmp1,tmp2=1.0f,result=0.0f;
int i;
for(i=n;i>=1;--i)
{
tmp1 = static_cast<double>(1)/static_cast<double>(i);
tmp2 = 1.0f+tmp1*tmp2;
}
return tmp2;
}
void caculateE(int n)
{//输出指定n下e的值,主要是输出格式的处理
double result = 0.0f;
result = doCaculate(n);
if(n==0||n==1)
{
cout<<n<<" "<<static_cast<int>(result)<<endl;
}
else if(n==2)
{
cout<<setiosflags(ios::fixed)<<setprecision(1);
cout<<n<<" "<<result<<endl;
}
else
{
cout<<setiosflags(ios::fixed)<<setprecision(9);
cout<<n<<" "<<result<<endl;
}
}
int main(int argc, char *argv[])
{
int n,i;
cout<<"n e"<<endl;
cout<<"- -----------"<<endl;
for(i=0;i<=9;++i)
{
caculateE(i);
}
return 0;
}
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2007-12-25 12:59 Phinecos(洞庭散人) 阅读(976) 评论(0) 编辑 收藏 举报