代码:
#include <bits/stdc++.h> using namespace std; const int maxn = 10005; int n, k, m; int a[maxn][15]; int ans[15]; int num = 0; void dfs(int sum, int x) // 总美味程度,已经选好了的配料种数 { if (sum > n) { return; } else if (sum == n && x > 10)//符合条件 { for (int i = 1; i <= 10; i++) { a[num][i] = ans[i]; } num++; return; } else if (sum != n && x > 10) { return; } ans[x] = 1; dfs(sum + 1, x + 1);//sum+1/2/3,位置右移 ans[x] = 2; dfs(sum + 2, x + 1); ans[x] = 3; dfs(sum + 3, x + 1); } int main() { cin >> n; if (n < 10 || n > 30) { cout << 0; return 0; } dfs(0, 1); cout << num<<endl; for (int j = 0; j < num; j++) { for (int i = 1; i <= 10; i++) { cout<<a[j][i]<<" "; } cout<<endl; } }