zoukankan      html  css  js  c++  java
  • 【递归与递推】排序集合

    【递归与递推】排序集合

    题目描述

    对于集合N={1,2,…,n}的子集,定义一个称之为“小于”的关系:
    设S1={X1,X2,…,Xi},(X1<X2<…<Xi),S2={Y1, Y2, …,Yj},(Y1<Y2<…<Yj),如果存在一个k,(0≤k≤min(i,j)),使得X1=Y1,…,Xk=Yk,且k=i或X(k+1)<Y(k+1),则称S1“小于”S2。
    你的任务是,对于任意的n(n≤31)及k(k<2n),求出第k小的子集。

    输入

    仅一行,包含两个用空格隔开的自然数,n和k。

    输出

    仅一行,使该子集的元素,由小到大排列。空集输出0。

    样例输入

    3 4
    

    样例输出

    1 2 3

    分析:暴搜即可
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #include <ext/rope>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define vi vector<int>
    #define pii pair<int,int>
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    const int maxn=1e2+10;
    const int dis[][2]={0,1,-1,0,0,-1,1,0};
    using namespace std;
    using namespace __gnu_cxx;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    int n,m,a[maxn],cnt;
    void dfs(int now,int all)
    {
        if(cnt==m-1)
        {
            printf("%d",a[0]);
            for(int i=1;i<all;i++)printf(" %d",a[i]);
            exit(0*printf("
    "));
        }
        cnt++;
        for(int i=now+1;i<=n;i++)
        {
            a[all]=i;dfs(i,all+1);
        }
    }
    int main()
    {
        int i,j,k,t;
        scanf("%d%d",&n,&m);
        if(m==1)puts("0");
        else dfs(0,0);
        //system("pause");
        return 0;
    }


  • 相关阅读:
    Three.js源码阅读笔记4
    算法导论11.动态规划上
    leetcode刷题笔录1
    JavaScript的模块化:封装(闭包),继承(原型)
    leetcode刷题笔录5
    算法导论1.排序算法
    算法导论3.递归部分习题选
    Three.js Demo源码分析1.MorphTargets与BufferGeometry
    算法导论10.顺序统计树与区间树习题
    算法导论4.随机快速排序与线性时间排序
  • 原文地址:https://www.cnblogs.com/dyzll/p/5645430.html
Copyright © 2011-2022 走看看