首页 > 其他分享 >洛谷P1157 组合的输出

洛谷P1157 组合的输出

时间:2023-01-15 19:35:20浏览次数:53  
标签:输出 include 洛谷 函数 int 排列 题目 P1157

原题链接

image

题解:

本题有两种办法解决,第一种 使用stl中 next_permutation函数

#include "iostream"
#include "algorithm"
#include "iomanip"
using namespace std;
int main(){
    int x,n,flag[25]={0};
    cin>>x>>n;
    for(int j=n;j<x;j++)flag[j]=1;
    do{
        for(int i=0;i<x;i++){
            if(flag[i]==false)
                cout<<setw(3)<<i+1;
        }
        cout<<endl;
    } while (next_permutation(flag,flag+x));
	//使用do while 输出本身,然后再求下一排列
}

next_permutation函数本来是用来求出给定范围内下一排列,但是在这道题目中有一个问题,
题目要求 在5个数里找3个数,而这个函数如果求1~5的全排列,那么就会输出以下内容:

1 2 3 4 5
1 2 3 5 4
1 2 4 3 5
1 2 4 5 3
等等

此时输出前三位就会出现重复 因为后两位也参与排序
所以 使用一个状态数组来进行排列,1代表不可输出,0代表可输出
然后使用内置函数对这个状态数组求排列,就可以实现题目要求
所以 以后需要求组合的时候 也可以使用这种办法

第二种 使用dfs

待后续补充

标签:输出,include,洛谷,函数,int,排列,题目,P1157
From: https://www.cnblogs.com/ChengMao/p/17053988.html

相关文章