题解 | #牛牛学数列3#
牛牛学数列3
https://www.nowcoder.com/practice/f65c726d081c4160a9356eabf0dc21d9
#include <stdio.h>
#include <math.h>
int main() {
double sum = 0, tempSum = 0;
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++) {
tempSum += pow((-1), (i - 1)) * (2 * i - 1); // 先把分母求和
sum += 1.0 / tempSum;
}
}
printf("%.3lf\n", sum);
return 0;
}