#include
int calc(int n)
{
if (0 == n || 1 == n)
{
return 1;
}
return n * calc(n -1);
}
void main()
{
int n = 0;
printf("请输入n的值:\n");
scanf("%d", &n);
double s = 0;
for (int i = 1; i <= n; i++)
{
//这里要注意下,需要转成浮点型相除,否则结果永远是0
s = 1.0/calc(n);
}
printf("s = %lf\n", s);
}
运行结果:
float calc(int n)
{
int i;
float r=1;
for(i=1;i<=n;i++)
r=r*1/i;
return r;
}
void main()
{
int n;
scanf("%d",&n);
printf("\n\n%f",calc(n));
}