当前位置:网站首页>n个数取出r个数排列
n个数取出r个数排列
2022-08-08 18:49:00 【-JMY-】
题目描述
从1~n任意挑出r个数进行排列,请从小到大输出所有可能的排列结果。
如:n=5,r=2,则输出结果如下
1 2
1 3
1 4
1 5
2 1
2 3
2 4
2 5
3 1
3 2
3 4
3 5
4 1
4 2
4 3
4 5
5 1
5 2
5 3
5 4
输入
两个整数n和r(n和r都是3~6之间的整数)
输出
从1~n中人去r个数的排列结果!
样例输入
5 2
样例输出
1 2 1 3 1 4 1 5 2 1 2 3 2 4 2 5 3 1 3 2 3 4 3 5 4 1 4 2 4 3 4 5 5 1 5 2 5 3 5 4
参考代码:
#include<bits/stdc++.h>
using namespace std;
int n,r,k[10];
bool a[10];
void pre(int l){
if(l>=r){
for(int i=1;i<=r;i++)
printf("%d ",k[i]);
printf("\n");
return;
}
for(int i=1;i<=n;i++){
if(!a[i]){
a[i]=true;
k[l+1]=i;
pre(l+1);
a[i]=false;
}
}
return;
}
int main(){
scanf("%d%d",&n,&r);
pre(0);
return 0;
}
边栏推荐
- USB CY68013设备描述符识别失败
- 进化的黑产 vs 进击的蚂蚁:支付宝的每一次点击,都离不开一张“图”的守护
- ABAP 报表中如何给报表的输入参数增添 F4 Value Help 试读版
- 架构设计基本原则
- 如何在Firewalld中为特定IP地址开放端口
- 能力一般,却可以大厂随便横跳?强在哪里?
- 在Unity URP中实现Forward+
- 证券开户选哪个券商平台比较好,哪个更安全
- The origin and creation of Smobiler's complex controls
- Is there any function in MAXCOMPUTE SQL to judge whether the string is a number?
猜你喜欢
随机推荐
JDBC最详讲解(快速入门)
Advanced CAD practice (2)
3D游戏建模教程:游戏角色制作——赏金猎人,超逼真
证券开户选哪个券商平台比较好,哪个更安全
性能问题从发现到优化一般思路
小白转行做3D游戏建模,有没有前途?
Rethinking HTAP database caused by rereading GPDB and TiDB papers
阿里云数据库PolarDB开源人才培养计划发布!万元好礼等你来拿!
Dataworks上的ODPS spark处理数据会比直接用ODPS SQL效率高吗?
The history of cartoon rendering
Oracle存储修改以前的历史记录,怎么查找?
Shell编程之循环语句与函数
数字化工厂建设的内容主要有哪三个方面
C语言初阶-结构体
一起了解分层架构&SOA架构
数据库学习之库的操作
请问在MAXCOMPUTE SQL 里有没有函数判断string 是否为数字?
面了个腾讯30k+出来的,他让我见识到什么叫精通MySQL调优
Why do programmers only close monitor from none computer after work?Look at the answer ~ each big web site
Excuse me, during the mongoshake synchronization process in the shake database, src_mongo hangs up, will the synchronization service not exit?









