HZOJ#236. Recursive implementation of combined enumeration

topic description

​ Select m randomly from the n integers 1−n, arrange the numbers in each scheme from small to large, and output all possible options in lexicographical order.


enter

​ Enter two integers n,m. (1≤m≤n≤10)

output

​ One set of schemes per line, and two numbers in each set of schemes are separated by spaces.

Note that there is no space after the last number on each line.


sample input

3 2

sample output

1 2
1 3
2 3

Sample input 2

5 3

Sample output 2

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

the code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
using namespace std;

// 定义一个长度为10的数组arr,用于存储数据  
int arr[10];  
  
// 定义一个函数print_one_result,用于打印数组arr的一种特定排列结果  
void print_one_result(int n) {  
    // 循环打印数组arr的前n个元素,每个元素之间用空格隔开  
    for (int i = 0; i < n; i++) {  
        if (i) cout << " "; // 如果不是第一个元素,打印一个空格  
        cout << arr[i]; // 打印当前元素  
    }  
    cout << endl; // 换行,打印下一个结果时从新的一行开始  
    return ; // 函数结束,返回  
}  
  
// 定义一个函数f,用于递归生成所有可能的数组排列  
void f(int i, int j, int n, int m) {  
    // 如果已经使用了m个不同的元素,打印当前排列结果并结束递归  
    if (i == m) {  
        print_one_result(m);  
        return ;  
    }  
    // 对于每个可能的元素k(从j开始到n),赋值给当前位置的元素,并递归调用f生成下一个位置的排列  
    for (int k = j; k <= n && m - i - 1 <= n - k; k++) {  
        arr[i] = k; // 将k赋值给当前位置的元素  
        f(i + 1, k + 1, n, m); // 递归调用f生成下一个位置的排列,j加1是因为当前位置已经使用了k,下一个位置的元素只能在k+1之后  
    }  
    return ; // 函数结束,返回  
}  
  
// 主函数,程序从这里开始执行  
int main() {  
    // 从用户输入中读取n和m的值  
    int n, m;  
    cin >> n >> m;  
    // 调用函数f开始生成排列结果  
    f(0, 1, n, m);  
    return 0; // 主函数结束,返回0表示程序正常结束  
}

This code is a C++ program whose purpose is to generate all possible array permutations such that the first m elements of the array are all distinct and each element is in the range 1 to n.

Specifically, the program first inputs the values ​​of n and m from the user. It then calls a function f which recursively generates all possible permutations.

In the function f, it first checks whether the end of the array has been reached (i.e. m different elements have been used). If so, then it prints out the current array arrangement and returns.

Otherwise, it iterates over the potential values ​​for the next element in the array. For each possible value k (starting at j, up to n), it assigns k to the element at the current position, and recursively calls the function f with the next position, k+1, as new j, and n as new m.

In the main function main, it starts generating permutations from user input of n and m values, and calls function f.

It should be noted that this program does not check the validity of the input, such as ensuring that m is less than or equal to n, etc. In practice, you may want to add these checks to prevent mistyping.

Guess you like

Origin blog.csdn.net/dsafefvf/article/details/132673329