问答题

请编写一个函数comm(int n,int k),该函数将用递归算法计算从n个人中选择k个人组成一个委员会的不同组合数,由n个人里选k个人的组合数=由(n-1)个人里选k个人的组合数+由(n-1)个人里选(k-1)个人的组合数。 注意:部分源程序已存在文件test41_2.cpp中。 请勿修改主函数main和其他函数中的任何内容,仅在函数comm的花括号中填写若干语句。 源程序文件test41-2.cpp清单如下: #include<iostream.h> int comm(int n, int k) { } void main ( ) { int n=7, k=3; cout<<"n=7,k=3"<<endl; cout<<comm(n,k)<<endl; }

【参考答案】

int comm(int n, int k) { if(k>n) return 0; else if(n==k||k......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
使用VC6打开考生文件夹下的工程test41_3。此工程包含一个test41_3.cpp,其中定义了类Rectangle,但该类的定义并不完整。请按要求完成下列操作,将程序补充完整。(1)定义类Rectangle的私有数据成员left,top和fight,bottom,它们都是int型的数据。请在注释“ **1**”之后添加适当的语句。(2)添加类Rectangle的带四个int型参数1、t、r、b的构造函数的声明,并使这四个参数的默认值均为0,请在注释“ **2**”之后添加适当的语句。(3)添加类Rectangle的成员函数SetTop()参数为int型的t,作用为把t的值赋给类的数据成员top,添加类Rectangle的成员函数SetBottom()参数为int型的t,作用为把t的值赋给类的数据成员bottom,请在注释“ **3**”之后添加适当的语句。(4)完成派生类Rectangle的成员函数Show()的定义,使其以格式“right-bottom point is(right,bottom)”输出,请在注释“ **4**”之后添加适当的语句。源程序文件test41_3.cpp清单如下:#include <iostream.h>class Rectangle{ ** 1 **int right, bottom;public: ** 2 **~ Rectangle(){};void Assign(int 1, int t, int r, int b);void SetLeft(int t){left = t;}void SetRight(int t){right = t;} ** 3 **void SetBottom(int t){bottom = t;}void Show();};Rectangle::Rectangle(int 1, int t, int r, int b){left = 1; top = t;right = r; bottom = b;}void Rectangle::Assign(int 1, int t, int r, int b){left = 1; top = t;right = r; bottom = b;}void Rectangle::Show(){cout<< left-top point is ( <<left<< , <<top<< ) <<’ n’; ** 4 **}void main(){Rectangle rect;rect.Show();rect.Assign(100,200,300,400);rect.Show();}