******************************先写第一个版本,假设手中的牌没有重复的*************************
#include<cstdio> #include<vector> #include<stack> #include<algorithm> using namespace std; #define MN 20 #define MM 10 int pai[MN+1]; struct rst { vector<int> data; void addElement(int &v_ele) { data.push_back(v_ele); } void print() { for (int i = 0; i < data.size(); i++) { if (i)printf(" "); printf("%d", data[i]); } } }; struct rstSet { vector<rst> data; void addRst(const rst &v_rst) { data.push_back(v_rst); } void clear() { data.clear(); } void addPreSet(rstSet v_preSet, int v_pai) { for each(const rst &singleRst in v_preSet.data) { rst newRst = singleRst; newRst.addElement(v_pai); data.push_back(newRst); } } }; rstSet dp[MM][MN]; void solve(int m,int n) { for (int j = 1; j < n; j++) { rstSet &theSet = dp[1][j]; theSet.clear(); rst newRst; newRst.addElement(pai[j]); theSet.addRst(newRst); dp[1][j] = theSet; } for (int i = 2; i <= m; i++) { for (int j = i; j <= n; j++) { rstSet &theSet = dp[i][j]; theSet.clear(); for (int k = i-1; k < j; k++) { theSet.addPreSet(dp[i - 1][k], pai[j]); } } } printf("答案:\n"); int cnt = 0; for (int i = m; i <= n; i++) { for (int j = 0; j < dp[m][i].data.size(); j++) { printf("#%d: ", ++cnt); dp[m][i].data[j].print(); printf("\n"); } } } int main() { int m, n; while (~scanf("%d%d", &m, &n)) { for (int i = 1; i <= n; i++) { scanf("%d", &pai[i]); } sort(pai, pai + n); solve(m, n); } return 0; } /* 3 5 1 2 3 4 5 5 10 1 2 3 4 5 6 7 8 9 10 */
时间: 2024-11-05 13:28:14