当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
21天学习挑战赛——机器学习01
最长子串(长沙理工大学第十一届程序设计竞赛 离线 做了n天.....崩溃了)
Open Office XML 格式中的 Style 设计原理
leetcode 240.搜索二维矩阵II 分治思想
Goose Factory Robot Dog Fancy Crossing 10m Plum Blossom Pile: Front Flip, Single Pile Jump, Get Up and Bow... No stumble in the whole process
Transsion Holdings: At present, there is no clear plan for the company's mobile phone products to enter the Chinese market
数字化工厂建设的内容主要有哪三个方面
轻量全景查看器 pannellum初探
This error is reported when the shake database is started. Is there a problem with the configuration?
SUSECON 北京议程上新丨8月16日相聚望京凯悦
Redis之SDS数据结构
如何在Firewalld中为特定IP地址开放端口
Learn about layered architecture & SOA architecture together
几何g6将搭载harmonyos系统,产品竞争力全面升级
APICloud AVM 封装日期和时间选择组件
A Preliminary Study on Pannellum, a Lightweight Panorama Viewer
SSM项目整合——综合案例
鹅厂机器狗花式穿越10m梅花桩:前空翻、单桩跳、起身作揖...全程不打一个趔趄
USB CY68013设备描述符识别失败
同花顺可以买股票吗?买股票安全吗?