求交错序列前N项和
https://pintia.cn/problem-sets/13/problems/410
1 #include <stdio.h> 2 int main(void) 3 { 4 int n; 5 int numerator, denominator; 6 int flag; 7 double item; 8 double sum; 9 10 numerator = 1; 11 denominator = 1; 12 flag = 1; 13 sum = 0; 14 scanf("%d", &n); 15 for (int i = 0; i < n; i++) 16 { 17 item = flag * 1.0 * numerator / denominator; 18 sum = sum + item; 19 numerator = numerator + 1; 20 denominator = denominator + 2; 21 flag = -flag; 22 } 23 24 printf("%.3f\n", sum); 25 26 return 0; 27 }