请编写递归函数,求阶乘。
n!=1×2×3×⋯×n
规定:0!=1
函数原型
long Fac(int x);
<svg xmlns="http://www.w3.org/2000/svg" class="pc-icon-raw" aria-hidden="true" style="box-sizing: border-box; fill: currentColor; flex-basis: auto; flex-grow: 0; flex-shrink: 0; font-size: 16px; height: 1em; margin-top: 0px; overflow: hidden; vertical-align: -0.15em; width: 1em;"><use style="box-sizing: border-box; margin-top: 0px;" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#pat-clippy"></use></svg>
说明:x
为任意整数。若 x 大于等于 0,则函数值为 x 的阶乘;否则函数值为 0。
裁判程序
#include <stdio.h>
long Fac(int x);
int main()
{
int n;
scanf("%d", &n);
printf("%ld\n", Fac(n));
return 0;
}
#####
输入输出示例
输入 | 输出 | |
示例 1 | | |
示例2 | 1 | 1 |
示例3 | 4 | 24 |
示例4 | -1 | 0 |
#include <stdio.h>
long Fac(int x)
{
int s=1,i;
if(x>=0)
{
for(i=1;i<=x;i++)
{
s=s*i;
}
}
else
{
s=0;
}
return s;
}
int main()
{
int n;
scanf("%d", &n);
printf("%ld\n", Fac(n));
return 0;
}