不好意思昨天忘了写,昨天是第八天
学习内容:
1.函数嵌套调用
2.函数递归调用
//函数执行到return后该函数中接下来的代码不执行
代码如下:
函数递归调用,求委员会组合数:
#include
using namespace std;
int cc(int n, int k)
{
int y;
if (n == k || k == 0)
y = 1;
else
y = cc(n - 1, k) + cc(n - 1, k - 1);
return y;
}
int main()
{
int n, k, y;
cout << “please enter n,k” << endl;
cin >> n >> k;
y = cc(n, k);
cout << “the c(n,k)=” << y << endl;
return 0;
}
函数递归调用,阶乘:
#include
using namespace std;
int fac(int n)
{
int f;
if (n == 0)
f = 1;
else
f = fac(n - 1) * n;
return f;
}
int main()
{
int n, y;
cout << “please enter a integer n”;
cin >> n;
y = fac(n);
cout << n << “!=” << y;
return 0;
}
今天事有点多,上午现代CAD课比较难,下课看那节课的ppt花了个把小时,晚上又有迎新晚会排练,就没学多少。