题解 | #牛牛学数列3#
牛牛学数列3
https://www.nowcoder.com/practice/f65c726d081c4160a9356eabf0dc21d9
#include <stdio.h> int main() { int n; double s = 0.0; //储存总数 int denominator = 0; //表示分母 int sign = 1; //表示变化的符号 scanf("%d",&n); for (int i = 1; i <=n; i++) { denominator = denominator+sign * (2*i-1);//0+1*(2*1-1)=1 s = s+1.0/denominator; //s = 0 + 1/1 = 1; sign = sign*(-1); } printf("%.3f\n",s); return 0; }