求精确值

寻找最小的正整数n,使得1/n与1/(n+1)的差小于0.0000001。输出n以及1/n、1/(n+1),保留小数点后8位。

#include<iostream>
#include <iomanip>
using namespace std;
int main()
{
 int n=1;
 double x;
 do
 {
  n++;
  x=1.0/n-1.0/(n+1);
 }
 while(x>=0.0000001);
 cout<<fixed<<setprecision(8);
 cout<<n<<"\n"<<1.0/n<<"\n"<<1.0/(n+1);
}
发布了109 篇原创文章 · 获赞 97 · 访问量 5113

猜你喜欢

转载自blog.csdn.net/huangziguang/article/details/104819298