编写函数borrow,实现下述功能:小明有五本新书,要借给A,B,C三位小朋友,若每人每次只能借一本,则可以有多少种不同的借法?要求将计算出的结果返回c++
时间: 2023-11-06 13:05:40 浏览: 34
以下是实现 borrow 函数的 C++ 代码:
```c++
#include <iostream>
using namespace std;
int borrow() {
int count = 0; // 记录借书方案数
for (int i = 0; i < 5; i++) {
for (int j = i + 1; j < 5; j++) {
for (int k = j + 1; k < 5; k++) {
count++; // 每遍历一种方案,计数器加一
}
}
}
return count;
}
int main() {
int result = borrow();
cout << "可以有 " << result << " 种不同的借法" << endl;
return 0;
}
```
该函数使用了三层嵌套的循环,枚举了所有三个小朋友借书的可能情况,并将计数器 count 加一。最后返回计数器的值,即为不同的借书方案数。在主函数中调用 borrow 函数并输出结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)