当前位置:网站首页>3428. 放苹果
3428. 放苹果
2022-07-27 08:37:00 【追寻远方的人】
把 M 个同样的苹果放在 N 个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?
盘子相对顺序不同,例如 5,1,1 和 1,5,1 算作同一种分法。
输入格式
输入包含多组测试数据。
每组数据占一行,包含两个整数 M 和 N。
输出格式
每组数据,输出一行一个结果表示分法数量。
数据范围
1≤M,N≤10
输入样例:
7 3
输出样例:
8
代码:
/* f(n,m) : 求n个盘子装m个苹果的方案数 找 比 n 小1的规模的方案数,来推出 n规模的方案数 如果找到了 n-1个盘子装 m个苹果的方案数,可能可以推 出n个盘子装m个苹果的方案数 1) M苹果<N 盘子 推出N-M个盘子不装苹果,拿掉这些盘子不影响方案数,剩下 f(n,m)=f( m ,m) 2) M苹果 >= N 盘子 f(n,m)=f(n-1,m)+f( n,m-n ) 分2种情况 (1) 一个盘子不装苹果 f(n,m)=f(n-1,m) (2) 第1种的相反情况 可以用 a&&b&&c&&d&&0 的相反情况 也就是 abcde都为1 每个盘子至少放1个球 每个盘子放1个球,一共放了n苹果,再把剩下的m-n个苹果放到n个盘子里 f(n,m)=f( n,m-n ) 边界情况 第0列 第1行都要初始化为1 */
#include <iostream>
using namespace std;
int f(int n, int m)
{
// 处理答案
if (n == 0 || m == 1)
return 1; // 没有苹果或者是盘子只剩一个
if (m > n)
return f(n, n); // 如果盘子数是大于苹果数的,多出来的盘子必定为空
else
return f(n, m - 1) + f(n - m, m);
// else代表盘子数小于等于苹果数,那就有两种情况
// (1) f(m,n - 1),有空盘,则至少有1个为空
// (2) f(m - n,n),无空盘,则每个盘子至少有一个苹果
}
int main()
{
int n, m;
while (cin >> n >> m)
{
cout << f(n, m) << endl;
}
return 0;
}
边栏推荐
- I drew a Gu ailing with characters!
- Using ecological power, opengauss breaks through the performance bottleneck
- Openresty + keepalived 实现负载均衡 + IPV6 验证
- "PHP Basics" tags in PHP
- JS basic exercises
- 面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?
- Make a game by yourself with pyGame 01
- Do a reptile project by yourself
- Solution of database migration error
- SSTI template injection
猜你喜欢

IBM3650M4实体机安装VCenter7.0

"PHP Basics" PHP statements and statement blocks

说透缓存一致性与内存屏障
![[MRCTF2020]PYWebsite 1](/img/d4/2d9cd06abd7188add668cde77d3075.png)
[MRCTF2020]PYWebsite 1

Hundreds of people participated. What are these people talking about in the opengauss open source community?

Have a good laugh

"Basic knowledge of PHP" implement mathematical operations in PHP

Vertical align cannot align the picture and text vertically

ERP production operation control Huaxia

VS Code中#include报错(新建的头文件)
随机推荐
Vcenter7.0 managing esxi7.0 hosts
regular expression
All in one 1329 cells (breadth first search)
How to view instances of software objects in QSIM?
Openresty + keepalived 实现负载均衡 + IPV6 验证
pytorch_demo1
Flink1.15 source code reading Flink clients client execution process (reading is boring)
Flink1.15源码阅读flink-clients客户端执行流程(阅读较枯燥)
vCenter7.0管理Esxi7.0主机
JS basic exercises
After installing mysql, docker entered the container and found that he could not log in to MySQL
Attack and defense World Lottery
Luogu super Mary game
Eval and assert execute one sentence Trojan horse
Flask login implementation
Using ecological power, opengauss breaks through the performance bottleneck
Fluent rendering mechanism - GPU thread rendering
【uni-app高级实战】手把手带你学习一个纯实战复杂项目的开发1/100
User management - restrictions
Process control - Branch