求一道c语言编程题的答案。。。。。很急~~~!

来源:百度知道 编辑:UC知道 时间:2024/05/13 23:54:56
编程求下面数列前10项的和,要求阶乘部分使用函数调用实现.
s=1/2!-2/3!+3/4!-4/5!+......+(-1)^(n-1)*[n/(n+1)!]

说明:(-1)^(n-1)表示-1的n-1次方;

#include <stdio.h>
int jc(int n)
{
if (n<=1) return 1;
else return n*jc(n-1);

}

int main()
{
double dresult = 0.0;
int k = 1, i;
for (i=1;i<=10;i++)
{
dresult+=((double)i)/((double)jc(i+1))*k;
k*=-1;
}
printf("%f",dresult);
}

随便写了一下,自己再修改一下
int func(int a, int b)
{
int c;
c = a;
b-=1;
while(b>0)
{
c*=a;
b--;
}
return c;
}