3088 - 1999年秋浙江省计算机等级考试二级C 编程题(1)
编制程序,输入m,n(M>=n>=0)后,计算下列表达式的值并输出:
m!
n! (m-n)!
要求将计算阶乘运算的函数写为fact(n),函数返回值的类型为float
Input
m n
Output
对应表达式的值
Examples
Input
2 1
Output
2
Solution C
#include <stdio.h> int fact(int n); int main(void) { int m,n,sum; scanf("%d %d",&m,&n); printf("%d\n",(fact(m))/(fact(n))/(fact(m-n))); return 0; } int fact(int n){ int i=1; int sum=1; while(i<=n){ sum*=i; ++i; } return sum; }
Solution C++
#include<iostream> #include<cstdio> using namespace std; float fact(int n) //求阶乘 { if (n==0 || n==1) return 1; float t=1; for (int i=1; i<=n; i++) t*=i; return t; } int main() { int m,n; cin>>m>>n; printf("%.0f\n",fact(m)/(fact(n)*fact(m-n))); return 0; }