题解 | #牛牛学数列3#
牛牛学数列3
https://www.nowcoder.com/practice/f65c726d081c4160a9356eabf0dc21d9
#include <stdio.h> //输入一个整数n,计算 1+1/(1-3)+1/(1-3+5)+...+1/(1-3+5-...((-1)^(n-1))*(2n-1))的值 //1 1-1/3 1-1/3+1/5...1-1/3+1/5-... int main() { int n = 0; scanf("%d", &n); int tmp = 0; double sum = 0; int flag=1; for (int i = 1; i <= n; i++) { tmp += flag * (2 * i - 1); sum += 1.0 / tmp; flag = -flag; } printf("%.3lf\n", sum); return 0; }
c语言刷题 文章被收录于专栏
c语言刷题题目