C++ 下列程序实现:x=1-1/22+1/333- 1/4444+1/55555......+1/999999999表达式的求值。
时间: 2024-01-29 20:02:45 浏览: 79
c语言多选1.docx
以下是C++实现该表达式求值的程序:
```cpp
#include <iostream>
#include <cmath>
int main() {
double x = 0;
double denominator = 2;
int sign = -1;
for (int i = 2; i <= 999999999; i++) {
x += sign * (1 / denominator);
denominator = denominator * 10 + i % 10;
sign *= -1;
}
std::cout << "x is " << x << std::endl;
return 0;
}
```
阅读全文