傻瓜式编程暴力解决,就是要注意数组得开的足够大否则会报RE
#include<iostream>
#include<algorithm>
#include<numeric>
using namespace std;
int n, sum = 0;
int p[10000][15], t[15];
int main()
{
cin >> n;
if (n < 10 || n>30) {
cout << 0;
}
else {
for (t[1] = 1; t[1] <= 3; t[1]++) {
for (t[2] = 1; t[2] <= 3; t[2]++) {
for (t[3] = 1; t[3] <= 3; t[3]++) {
for (t[4] = 1; t[4] <= 3; t[4]++) {
for (t[5] = 1; t[5] <= 3; t[5]++) {
for (t[6] = 1; t[6] <= 3; t[6]++) {
for (t[7] = 1; t[7] <= 3; t[7]++) {
for (t[8] = 1; t[8] <= 3; t[8]++) {
for (t[9] = 1; t[9] <= 3; t[9]++) {
for (t[10] = 1; t[10] <= 3; t[10]++) {
if (accumulate(t + 1, t + 11, 0) == n) {
for (int i = 1; i <= 10; i++) {
p[sum][i] = t[i];
}
sum++;
}
}
}
}
}
}
}
}
}
}
}
cout << sum << endl;
for (int i = 0; i < sum; i++) {
for (int j = 1; j <= 10; j++) {
cout << p[i][j];
if (j != 10)
cout << " ";
}
cout << endl;
}
}
return 0;
}