1+1/(1+2)+1/(1+2+3)+1/(1+2+3+4)+……+1/(1+2+3+4+……+n)
时间: 2023-10-05 08:08:12 浏览: 46
题目要求计算级数1/(1*2*3*...*n),其中n为输入的通项数。可以使用递归或循环来计算这个级数。
递归算法:
```c
#include <stdio.h>
double calcSeries(int n) {
if (n == 1)
return 1.0;
else
return calcSeries(n - 1) / n;
}
int main() {
int n;
scanf("%d", &n);
printf("%lf", calcSeries(n));
return 0;
}
```
循环算法:
```c
#include <stdio.h>
int main() {
int n;
double sum = 0.0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
double term = 1.0;
for (int j = 1; j <= i; j++) {
term *= j;
}
sum += 1.0 / term;
}
printf("%lf", sum);
return 0;
}
```
阅读全文