这题wa了好几次都是因为output数组不够大,第一维至少要开到10000.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include<stack>
#define ll long long
using namespace std;
int ans[20];
int output[10000][11];
int n;
int p;
int num = 0;
void dfs(int l)
{
if (l > 10)return;
for (int i = 1; i <= 3; i++)
{
if (n - i >= 0)
{
n -= i;
ans[l] = i;
if (l == 10 && n==0)
{
for (int j = 1; j <= 10; j++)
{
output[num][j] = ans[j];
}
num++;
}
else dfs(l + 1);
n += i;
}
}
}
int main()
{
cin >> n;
p = n;
if (n < 10||n>30)
{
printf("0\n");
return 0;
}
dfs(1);
printf("%d\n", num);
for (int i = 0; i < num; i++)
{
for (int j = 1; j <= 10; j++)
{
printf("%d ", output[i][j]);
}
printf("\n");
}
}