浙大c程序语言设计(第2版)题库答案
1! = 1
2! = 2
3! = 6
#include <stdio.h>
int main(void)
{
int i, n;
double myfact;
double fact(int n);
scanf("%d", &n);
for(i=1;i<=n;i++) {
myfact=fact(i);
printf("%d! = %.0f\n", i, myfact);
}
}
double fact(int n)
{
double result;
int j;
result=1;
for(j=1;j<=n;j++)
result=result*j;
return result;
}
20043 使用函数求 n! /(m!* (n-m)!)
输入一个正整数repeat (0<repeat<10),做repeat次下列运算:
输入2个正整数 m 和 n(m<=n),计算 n! /(m!* (n-m)!) 。
要求定义并调用函数fact(n)计算n的阶乘, 其中 n 的类型是 int,函数类型是 double。 例:括号内是说明
输入:
2 (repeat=2)
2 7 (m=2, n=7)
5 12 (m=5, n=12)
输出:
result = 21
result = 792
#include "stdio.h"
int main(void)
{
int m, n;