<>一、问题描述

<>二、题解
#include <iostream> #include <vector> using namespace std; class Solution {
private: vector<vector<int>> result; // 结果集 int five_count = 0; //

queueShop(int n) { vector<int> each_case; backtracking(each_case, n); return
result; } void backtracking(vector<int> &each_case, int n) { if (each_case.size(
) == 2 * n) { result.emplace_back(each_case); return; } /*

five_count++; backtracking(each_case, n); each_case.pop_back(); five_count--; }
/* 只有当前队列中0.5的数目多于1时才考虑加入1 */ if (one_count < n && five_count > one_count) {
each_case.emplace_back(1); one_count++; backtracking(each_case, n); each_case.
pop_back(); one_count--; } } }; int main(int argc, char *argv[]) { Solution
solution; auto res = solution.queueShop(5); for (const auto &i : res) { for (
const auto &j : i) { cout << j << " "; } cout << endl; } }
<>三、运行结果
atreus@MacBook-Pro % clang++ main.cpp -o main -std=c++11 atreus@MacBook-Pro %
./main5 5 5 5 5 1 1 1 1 1 5 5 5 5 1 5 1 1 1 1 5 5 5 5 1 1 5 1 1 1 5 5 5 5 1 1 1
5 1 1 5 5 5 5 1 1 1 1 5 1 5 5 5 1 5 5 1 1 1 1 5 5 5 1 5 1 5 1 1 1 5 5 5 1 5 1 1
5 1 1 5 5 5 1 5 1 1 1 5 1 5 5 5 1 1 5 5 1 1 1 5 5 5 1 1 5 1 5 1 1 5 5 5 1 1 5 1
1 5 1 5 5 5 1 1 1 5 5 1 1 5 5 5 1 1 1 5 1 5 1 5 5 1 5 5 5 1 1 1 1 5 5 1 5 5 1 5
1 1 1 5 5 1 5 5 1 1 5 1 1 5 5 1 5 5 1 1 1 5 1 5 5 1 5 1 5 5 1 1 1 5 5 1 5 1 5 1
5 1 1 5 5 1 5 1 5 1 1 5 1 5 5 1 5 1 1 5 5 1 1 5 5 1 5 1 1 5 1 5 1 5 5 1 1 5 5 5
1 1 1 5 5 1 1 5 5 1 5 1 1 5 5 1 1 5 5 1 1 5 1 5 5 1 1 5 1 5 5 1 1 5 5 1 1 5 1 5
1 5 1 5 1 5 5 5 5 1 1 1 1 5 1 5 5 5 1 5 1 1 1 5 1 5 5 5 1 1 5 1 1 5 1 5 5 5 1 1
1 5 1 5 1 5 5 1 5 5 1 1 1 5 1 5 5 1 5 1 5 1 1 5 1 5 5 1 5 1 1 5 1 5 1 5 5 1 1 5
5 1 1 5 1 5 5 1 1 5 1 5 1 5 1 5 1 5 5 5 1 1 1 5 1 5 1 5 5 1 5 1 1 5 1 5 1 5 5 1
1 5 1 5 1 5 1 5 1 5 5 1 1 5 1 5 1 5 1 5 1 5 1 atreus@MacBook-Pro %

GitHub

Gitee