求大神解答,这个要怎么写
输入n,求下列级数前n项的和。1/2-2/3+3/5-5/8……
程序代码:
#include <stdio.h> #include <stdlib.h> double fun1(int n) { int i, k = -1; double sum = 0; long a = 1, b = 2, t; for(i = 1; i <= n; i++) { k = k * -1; sum += (double)k * a / b; printf("%c %ld/%ld ", k < 0 ? '-' : '+', a, b); t = a; a = b; b = t + b; } return sum; } int main() { int n; printf("计算1/2-2/3+3/5-5/8……\n输入n:"); scanf("%d", &n); printf("= %lf ", fun1(n)); system("pause > nul"); return 0; }