C语言编写程序求S=1!-2!+3!+...(-1)N! N由键盘输入 输出S的值

2024-11-05 20:09:42
推荐回答(2个)
回答1:

VC6调试通过
----------------------------------
#include

main()
{
int n;
float s=0,t=1,k=1;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
t*=i;
s+=t*k;
k*=-1;
}
printf("1-2!+3!...+(-1)%d-1*%d!=%f\n",n,n,s);
}

回答2:

我来实现:
#include
unsigned long func(int n)
{
int tmp=1;
unsigned long res = 1;
for(int i=2;i<=n;++i)
{
tmp *= (-1*i);
res += tmp;
}
return res;
}
void main()
{
int n;
cin>>n;
cout<}