P1157 组合的输出

// Problem: P1157 组合的输出
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1157
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// User: Pannnn

#include <bits/stdc++.h>

using namespace std;

// 二进制枚举
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n, r;
    cin >> n >> r;
    
    vector<vector<int>> res;
    for (int i = 0; i < (1 << n); ++i) {
        vector<int> tmp;
        for (int j = 0; j < n; ++j) {
            if ((i & (1 << (n - j - 1))) != 0) {
                tmp.push_back(j + 1);
            }
        }
        if (tmp.size() == r) {
            res.push_back(tmp);
        }
    }
    sort(res.begin(), res.end());
    for (int i = 0; i < res.size(); ++i) {
        for (int j = 0; j < res[i].size(); ++j) {
            printf("%3d", res[i][j]);
        }
        printf("\n");
    }
    return 0;
}
posted @ 2022-02-07 15:18  Pannnn  阅读(42)  评论(0)    收藏  举报
-->