题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1012
return sum(n-1)+curItem(n);
double tmp1,tmp2=1.0f,result=0.0f;
cout<<n<<" "<<static_cast<int>(result)<<endl;
cout<<setiosflags(ios::fixed)<<setprecision(1);
cout<<n<<" "<<result<<endl;
cout<<setiosflags(ios::fixed)<<setprecision(9);
cout<<n<<" "<<result<<endl;
int main(int argc, char *argv[])
cout<<"- -----------"<<endl;
解法二:题中给出的计算e的式子是由e^x的泰勒级数展开而得,在计算之前可以使用个技巧,就是把它们叠乘起来改写成:
e=(1+(1+1/2(1+1/3(1+1/4(1+…1/(n-1)(1+1/n))))),从最里面的括号往外算,共做n次除法和加法得一段结果,运算效率也是O(N*M),但是由于收敛速度快些,所以N项节省一些,
double tmp1,tmp2=1.0f,result=0.0f;
tmp1 = static_cast<double>(1)/static_cast<double>(i);
cout<<n<<" "<<static_cast<int>(result)<<endl;
cout<<setiosflags(ios::fixed)<<setprecision(1);
cout<<n<<" "<<result<<endl;
cout<<setiosflags(ios::fixed)<<setprecision(9);
cout<<n<<" "<<result<<endl;
int main(int argc, char *argv[])
cout<<"- -----------"<<endl;
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/12/25/1013925.html,如需转载请自行联系原作者