#include <stdio.h> #include <math.h> int main() { int n = 0;//定义项数 scanf("%d", &n); int i = 0;//遍历 int x = 1; int t = 0; double sum = 0; for (i = 1; i <= n; i++)//项数 { t += (2*i-1)*x; sum += 1.0/t; x = -x; } printf("%.3lf\n", sum); return 0; }
#include <stdio.h> #include <math.h> int main() { int n = 0;//定义项数 scanf("%d", &n); int i = 0;//遍历 int x = 1; int t = 0; double sum = 0; for (i = 1; i <= n; i++)//项数 { t += (2*i-1)*x; sum += 1.0/t; x = -x; } printf("%.3lf\n", sum); return 0; }