返回题解分享
讨论 / 题解分享/ 帖子详情

部分元素排列 - 题解

#include<bits/stdc++.h>
#define int long long 
#define endl '\n'
using namespace std;
int n,r;
int nums[10];
bool st[10];
int ans[10];
bool flag = true;
void dfs(int accounts){
	if(accounts == r){
		for(int i = 0; i < r - 1;i++){
			if(ans[i] > ans[i+1]){
			flag = false;
		}
		}
		if(flag == true){
		for(int i = 0;i < r;i++){
			cout<<ans[i]<<" ";
		}
		cout<<endl;
	}
	flag = true;
		return ;
	}
	for(int i = 0;i < n;i++){
		if(st[i] == false){
		ans[accounts] = nums[i];
		st[i] = true;
		dfs(accounts+1);
		st[i] = false;
		}
	}
	return ;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	cin>>r;
	for(int i = 0;i < n;i++){
		cin>>nums[i];
	}
	sort(nums,nums+n);
	dfs(0);
	return 0;
}
0 回复 0 转发 0 喜欢 2 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!