You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
3.1 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
//方案数量,预计,注意此处可不是n的数据上限而是可能的方案数量尽量开大防止出错黄海就因为开小了挂掉
const int M = 20; //配料的数量本题目是10为了开大一点数组加了10
int a[N][M]; //保存所有可行的方案,描述第i种方案放过二维的第一个格子0第二个格子1表示i种方案中第一个配料用多少克
int path[M]; //记录当前正在计算的方案.本题经验数组开小了会出错n值错乱的诡异问题描述当前方案每一种配料用多少克
/**
C++访/
使
*/
/**
dfs使广
dfs
*/
int n, cnt; //美味值,可行方案数量
/**
*
* (1)stepsum
* (2)step123
* (3)i
* step+1sum+i
* (4)10step=9(step0)
*/
//step为第几种调料sum目前的配料之和
void dfs(int step, int sum) {
if (step == 11) { //当10种调料都分析完了
//如果配料之和等于输入值;
//使用memcpy更简单,参数:目标数组,源数组,长度
if (sum == n)memcpy(a[cnt++], path, sizeof path);
//不管等不等,都需要返回了,递归的出口
return;
}
//在当前step下就是在讨论某一种调料时面临三种选择123
for (int i = 1; i <= 3; i++) {
path[step] = i; //第step种调料的配料为i
dfs(step + 1, sum + i); //递归,对下一种调料做同样的分析
}
}
int main() {
cin >> n;
//从第1步开始目前总和是0
dfs(1, 0);
//输出方案个数
cout << cnt << endl;
//再输出具体的方案
if (cnt > 0) {
for (int i = 0; i < cnt; i++) {
for (int j = 1; j <= 10; j++)
cout << a[i][j] << " ";
cout << endl;
}
}
return 0;
}