当前位置:网站首页>【递归之数的拆分】n分k,限定范围的拆分
【递归之数的拆分】n分k,限定范围的拆分
2022-04-23 15:32:00 【MC快乐的苦小怕】
题目自己编的
1.n分k
Ⅰ:
N拆分K项①
题目描述
问题描述
有N颗弹珠要分给K个人,给定N、K,求有多少种分配方案。
输入描述
一行,两个整数,为N、K
输出描述
一个整数,表示分配的方案数。
输入样例
7 3
输出样例
36
数据范围
1<=K<=8
K<=N<=30
提示
4个球分给三个人,有15种分配方案
0 0 4
0 1 3
0 2 2
0 3 1
0 4 0
1 0 3
1 1 2
1 2 1
1 3 0
2 0 2
2 1 1
2 2 0
3 0 1
3 1 0
4 0 0
注意:1 3 5, 1 5 3, 3 5 1算为3种方案
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <list>
#include <limits.h>
using namespace std;
int cnt=0;
int n,k;
void doit(int x,int step) //x是剩下的乒乓球数量,step是当前分配到的是第几个人
{
if(step==k){
//个数到了,不能分了,该“归”了!
cnt++; //算为一种方案
return; //继续寻找方案qwq
}
for(int i=0;i<=x;i++)doit(x-i,step+1); //从每个人最少的0开始,列举每一种可行的方案,并最终得到答案
}
int main()
{
cin>>n>>k;
doit(n,1);
cout<<cnt;
//直接出结果
return 0;
}
题目描述
N个乒乓球,分成K堆,问有多少种不同的分法。
例如:n=7,k=3,下面三种划分方案被认为是相同的。
1 1 5
1 5 1
5 1 1
样例输入
7 3
样例输出
4
提示
6<N<=200
2<=K<=6
这里题目明确说明了这个是不能有那种重复的分配方法的,所以该怎么分配n?
这题其实就是一道递推的放苹果,只不过就是稍稍有一点变化。
递归解法①
int apple(int m,int n){
//纯递推思路
if(m==0||n==1) return 1;
if(n>m) return apple(m,m);
else return apple(m,n-1)+apple(m-n,n);
}
递归解法②
void apple(int pre,int n,int step){
//递归的思路
if(step==1) {
cnt++;
return;
}
for(int i=pre;i<=n/step;i++)apple(i,n-i,step-1);
}
限定范围的拆分①
题目描述
问题描述
有N颗珍珠要分给K个人,要求每个人最少为MIN颗,最多为MAX颗。给定N、K、MIN、MAX,求满足分配要求的方案数,若没有合适的方案,则输出0。
输入描述
一行,四个整数,分别表示N、K、MIN、MAX
输出描述
一个整数,表示符合分配要求的方案数。
输入样例
7 3 1 4
输出样例
12
数据范围
K<=N<=30
数据范围
1<=K<=8
K<=N<=30
注意:1, 3, 5 1, 5, 3 5, 3, 1 算为3种方案
这道题竟然限制了上和下的边界,其实我们只需要把上下边界带入问题,就很容写出来了
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <list>
#include <limits.h>
using namespace std;
int cnt=0,maxn,minn;
int n,k;
void doit(int x,int step)
{
if(step==k){
if(x>=minn && x<=maxn)cnt++; //判断此方案是否合法
return; //不管合不合法还是要继续寻找新的方案
}
for(int i=minn;i<=min(maxn,x);i++)doit(x-i,step+1); //如果剩下的还不足maxn,那么只能列举到x(也就是min(maxn,x))
}
int main()
{
cin>>n>>k>>minn>>maxn;
doit(n,1);
cout<<cnt;
return 0;
}
限定范围的拆分②
题目描述
问题描述
有N个乒乓球分成K堆,要求每堆最少为MIN个,最多为MAX个。给定N、K、MIN、MAX,求满足分配要求的方案数,若没有合适的方案,则输出0。
输入描述
一行,四个整数,分别表示N、K、MIN、MAX
输出描述
一个整数,表示符合分配要求的方案数。
输入样例
7 3 1 4
输出样例
3
数据范围
K<=N<=30
数据范围
1<=K<=8
K<=N<=30
注意:1, 3, 5 1, 5, 3 5, 3, 1 算为1种方案
同样,代码上:
void doit(int x,int step,int o)
{
if(step==k){
if(x>=max(o,minn) && x<=maxn)cnt++;
return;
}
for(int i=max(o,minn);i<=min(maxn,x);i++)doit(x-i,step+1,i);
}
版权声明
本文为[MC快乐的苦小怕]所创,转载请带上原文链接,感谢
https://xiaoguogsc.blog.csdn.net/article/details/124257703
边栏推荐
- TLS / SSL protocol details (30) RSA, DHE, ecdhe and ecdh processes and differences in SSL
- php类与对象
- Connect PHP to MSSQL via PDO ODBC
- Common types of automated testing framework ▏ automated testing is handed over to software evaluation institutions
- regular expression
- Detailed explanation of MySQL connection query
- 什么是CNAS认证?CNAS认可的软件测评中心有哪些?
- 控制结构(二)
- Educational codeforces round 127 A-E problem solution
- Use of common pod controller of kubernetes
猜你喜欢

T2 icloud calendar cannot be synchronized

How did the computer reinstall the system? The display has no signal

Mumu, go all the way
![Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]](/img/07/c534238c2b5405bbe4655e51cfee51.png)
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]

Reptile exercises (1)

Tun model of flannel principle

Mysql database explanation (10)

Sword finger offer (1) -- for Huawei

移动金融(自用)

机器学习——逻辑回归
随机推荐
el-tree实现只显示某一级复选框且单选
Recommended search common evaluation indicators
自主作业智慧农场创新论坛
Connect PHP to MySQL via PDO ODBC
Nacos program connects to mysql8 0+ NullPointerException
Async void caused the program to crash
The life cycle of key value in redis module programming
Deeply learn the skills of parameter adjustment
Node.js ODBC连接PostgreSQL
C language super complete learning route (collection allows you to avoid detours)
Application of skiplist in leveldb
【Leetcode-每日一题】安装栅栏
Connectez PHP à MySQL via aodbc
Three uses of kprobe
Llvm - generate for loop
如何设计一个良好的API接口?
服务器中毒了怎么办?服务器怎么防止病毒入侵?
The wechat applet optimizes the native request through the promise of ES6
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
Hj31 word inversion