当前位置:网站首页>ACM. HJ61 放苹果 ●
ACM. HJ61 放苹果 ●
2022-06-21 19:45:00 【chenyfan_】
HJ61 放苹果 ●
描述
把m个同样的苹果放在n个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?
注意:如果有7个苹果和3个盘子,(5,1,1)和(1,5,1)被视为是同一种分法。
数据范围: 0 ≤ m ≤ 10 0 \le m \le 10 0≤m≤10 , 1 ≤ n ≤ 10 1 \le n \le 10 1≤n≤10。
输入描述:
输入两个int整数
输出描述:
输出结果,int型
示例
输入:
7 3
输出:
8
题解
1. 动态规划
- dp[i][j] 表示有 i 个苹果,j 个盘子时的不同方法数;
dp[0][j] = 1;0个苹果,一种方法;dp[i][1] = 1;1个盘子,一种方法。- 当 i < j 即苹果树小于盘子数时,一定存在空盘子,继承
dp[i][j] = dp[i][j-1];
当 i >= j 即苹果数大于等于盘子数时,dp[i][j] = dp[i][j-1] + dp[i-j][j];分两种情况:
(1)有一个空盘子:dp[i][j-1];(往下递推可能还包括其他空盘子)
(2)无空盘子:dp[i-j][j];每个盘子都放一个苹果,即等价于 剩余 i - j 个苹果放在 j 个盘子的方法数。 - 外层遍历苹果数,内层遍历盘子数,从小到大。
- 时间复杂度: O ( m ∗ n ) O(m∗n) O(m∗n),进行两层循环,通过穷举需要进行两层 for 循环实现递推
- 空间复杂度: O ( m ∗ n ) O(m∗n) O(m∗n),维护二维数组保存状态

#include <iostream>
#include <vector>
using namespace std;
int main(){
int apple, plate;
cin >> apple >> plate;
int dp[apple+1][plate+1];
for(int j = 0; j <= plate; ++j) dp[0][j] = 1; // 0个苹果
for(int i = 1; i <= apple; ++i) dp[i][1] = 1; // 1个盘子
for(int i = 1; i <= apple; ++i){
// 苹果数 i
for(int j = 2; j <= plate; ++j){
// 盘子数 j
if(i < j){
dp[i][j] = dp[i][j-1]; // 当 i < j 即苹果树小于盘子数时,一定存在空盘子,继承 `dp[i][j] = dp[i][j-1];`
}else{
dp[i][j] = dp[i][j-1] + dp[i-j][j];
// 当 i >= j即苹果数大于等于盘子数时况:
//(1)有一个空盘子:`dp[i][j-1];`(dp[i][j-1];中可能还包括其他空盘子)
//(2)无空盘子:`dp[i-j][j];`每个盘子都放一个苹果,即等价于 剩余 i - j 个苹果放在 j 个盘子的方法数。
}
}
}
cout << dp[apple][plate] << endl;
return 0;
}
2. 递归
情况分析类似于动态规划,此处利用递归进行计算。
- 时间复杂度: O ( 2 n ) O(2^n) O(2n),树型递归,最大深度为N,总共递归 2 N 2^N 2N 次
- 空间复杂度: O ( n ) O(n) O(n),递归栈的深度不超过树高,即不超过盘子数 n
#include <iostream>
#include <vector>
using namespace std;
int count(int apple, int plate){
if(apple == 0 || plate == 1) return 1;
if(apple < plate){
return count(apple, plate-1);
}else{
return count(apple, plate-1) + count(apple-plate, plate);
}
}
int main(){
int apple, plate;
cin >> apple >> plate;
cout << count(apple, plate) << endl;
return 0;
}
边栏推荐
- MySQl学习(从入门到精通 1.2)
- What kind of person are you in life?
- Xshell7+Xftp7免费版下载
- What can one line of code do?
- 集群一---LVS负载均衡集群NAT模式及LVS负载均衡实战部署
- PowerPoint 教程,如何在 PowerPoint 中将幻灯片整理成组?
- 自己动手写编译器:while,for,do等循环语句的中间代码生成
- Leecode70 climbing stairs
- 2022 National latest fire facility operator (intermediate fire facility operator) simulation question bank and answers
- ARP协议及ARP攻击
猜你喜欢

libtorch显存管理示例

C语言数组与指针练习题(原题+解析+原码)

The first in the industry! Krypton app has obtained the authoritative certification of China Network Security Review Technology and Certification Center

The latest simulation test questions and answers of Henan construction electrician (special construction operation) in 2022

Intersection of vector and plane

Libtorch video memory management example

Product innovation - an innovative social app that returns to real life

【微服务七】Ribbon负载均衡策略之BestAvailableRule源码深度剖析

Delaying patient self-help guide | "I have 100 excuses for not delaying, but I am not willing to take a step"

【MySQL·水滴计划】第三话- SQL的基本概念
随机推荐
ASP.Net Core创建Razor页面上传多个文件(缓冲方式)
LVS+Keepalived高可用群集实战部署
Is it true and safe for qiniu to open a securities account? Do you charge for opening an account
js的对象操作(与c的对象比较简单的多)
Welcome to the markdown editor
unity动态读取外部音乐并播放
腾讯全球数字生态大会-高速智能计算专场!
MySQl学习(从入门到精通 1.2)
C语言回调函数到底是怎么回事?
MySQL learning (from getting started to mastering 1.2)
十一、美化界面
2016 ICLR | Adversarial Autoencoders
J - Count the string HDU - 3336 (KMP)
基于 PCA 的人脸识别系统及人脸姿态分析
PC e-commerce platform - search module
What are some tricks that novice programmers don't know?
2022年全国最新消防设施操作员(中级消防设施操作员)模拟题库及答案
修改UE4缓存路径,缓解c盘压力
Common cases of hybrid cloud exercises
【小程序】通过request实现小程序与后台asp.net的数据json传输(Post协议 图文+代码)