编程计算:1!+3!+5!+…+(2n-1)!的值。其中,n值由键盘输入 变量定义部分让你已给出如下

#includevoid main(){int i,b=1,s=0,n;printf("Please input n=");scanf("%d",&n);.........}
2024-12-21 23:57:18
推荐回答(3个)
回答1:

main()
{
int i,b=1,s=0,n;
printf( "please input n=");
scanf("%d",&n);
if(n>1)
for(i=1;i<2n-1;i+=2)
{
b*=(i+1)*(i+2);
s=s+b;
}
s=s+1;
printf("%d,s");
}

手机发的,可能有些粗糙,如果能提交的话,望采纳。谢谢(^_^)

回答2:

这个应该是用循环做,而不是用递归。我的代码如下,应该符合要求:

#include 
void main()
{
int i,b=1,s=0,n;
printf("Please input n=");
scanf("%d",&n);
for (i=n; i>=1; i--)
{
for (n=1; n<=2*i-1; n++)
b *= n;
s += b;
b = 1;
}
printf("s = %d\n", s);
}

回答3:

#include
int func(int n)
{
    if(n == 1)
        return 1;
    else return n*func(n - 1);
}
int main()
{
    int sum = 0,n;
    printf("Please input n=");
    scanf("%d",&n);
    for(int i = 1 ; i <= n ; i++)
        sum += func(2*i -1 );
    printf("SUM = %d\n",sum);
    return 0;
}