编一程序,利用公式求π的值,公式为: π/4=1-1/3+1/5-1/7+ ......+1/(4*n-3)-1/(4*n-1) (n=10000 按double计算保证计算精度)
时间: 2023-09-08 21:12:58 浏览: 98
```c++
#include <iostream>
using namespace std;
int main() {
double pi = 0.0;
int n = 10000;
for (int i = 0; i < n; i++) {
double a = 1.0 / (4 * i + 1);
double b = 1.0 / (4 * i + 3);
pi += a - b;
}
pi *= 4;
cout << "π的近似值为:" << pi << endl;
return 0;
}
```
阅读全文