1
0
mirror of https://github.com/renbaoshuo/202401-programming-assignments.git synced 2024-12-16 15:44:39 +00:00
202401-programming-assignments/【实践课内】6.循环结构3/7-1 求简单交错序列前N项和.c

21 lines
308 B
C
Raw Normal View History

2024-11-01 08:36:33 +00:00
#include <stdio.h>
int main() {
int n;
double ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
ans += 1.0 / (1 + 3 * i);
} else {
ans -= 1.0 / (1 + 3 * i);
}
}
printf("sum = %.3lf\n", ans);
return 0;
}