• 递归实现组合型枚举


    code

    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define rep(i,a,b) for(int i=(a);i<=(b);++i)
    #define dwn(i,a,b) for(int i=(a);i>=(b);--i)
    
    using pii=pair<int,int>;
    using ll=long long ;
    const int N=50;
    int a[N];
    int n,k;
    
    inline void read(int &x){
    	int  s=0;
    	x=1;
    	char ch=getchar();
    	while('0'>ch||'9'<ch){
    		if('-'==ch){
    			x-=1;
    		}
    		ch=getchar();
    	}
    	while('0'<=ch&&'9'>=ch){
    		s=(s<<3)+(s<<1)+ch-'0',
    		ch=getchar();
    	}
    	x*=s;
    }
    int main(){
    	ios::sync_with_stdio(false);
    	cin.tie(0);
    	//int n,k;
    	cin>>n>>k;
    	rep(i,1,k) {
    		a[i]=1;
    	}
    	do{
    		rep(i,1,n){
    			if(a[i]){
    				cout<<i<<' ';
    			}
    		}
    		cout<<endl;
    	}while(prev_permutation(a+1,a+1+n));
    	return 0;
    }
    

    Q

       题目
       提交记录
       讨论
       题解
       视频讲解
    
    从 1∼n 这 n 个整数中随机选出 m 个,输出所有可能的选择方案。
    
    输入格式
    两个整数 n,m ,在同一行用空格隔开。
    
    输出格式
    按照从小到大的顺序输出所有方案,每行 1 个。
    
    首先,同一行内的数升序排列,相邻两个数用一个空格隔开。
    
    其次,对于两个不同的行,对应下标的数一一比较,字典序较小的排在前面(例如 1 3 5 7 排在 1 3 6 8 前面)。
    
    数据范围
    n>0 ,
    0≤m≤n ,
    n+(n−m)≤25
    输入样例:
    5 3
    输出样例:
    1 2 3 
    1 2 4 
    1 2 5 
    1 3 4 
    1 3 5 
    1 4 5 
    2 3 4 
    2 3 5 
    2 4 5 
    3 4 5 
    
  • 相关阅读:
    xp_cmdshell
    常用SQL语句
    SQL Server Select的递归查询-交叉表
    Sql Server 2005 行转列的实现(横排)
    sql导入导出
    使用正则表达式验证手机号、车牌号
    页面功能:设为首页和加入收藏
    两个文本框同步输入
    最常用的200个JS代码
    .NET 获取时间
  • 原文地址:https://www.cnblogs.com/jeseesmith/p/15983831.html
Copyright © 2020-2023  润新知