题目链接:
不要怕太暴力而不敢打()
第一时间想到的其实是深搜,每个位置都有三种选择,考虑完当前位置后再顺次考虑接下来的位置。后来仔细一想,这不就是暴力枚举么!直接十重循环,每次看各个位置上的数之和是否等于 \(n\),时间复杂度为 \(3^{10}\) 约为 \(5e^4\), 因此不会超时。
#include <cstdio>
#include <vector>
int main()
{
int n, cnt = 0;
scanf("%d", &n);
std::vector<std::vector<int>> v;
for (int a = 1; a <= 3; a++) {
for (int b = 1; b <= 3; b++) {
for (int c = 1; c <= 3; c++) {
for (int d = 1; d <= 3; d++) {
for (int e = 1; e <= 3; e++) {
for (int f = 1; f <= 3; f++) {
for (int g = 1; g <= 3; g++) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
for (int k = 1; k <= 3; k++) {
if (a + b + c + d + e + f + g + i + j + k == n) {
cnt++;
std::vector<int> p;
p.push_back(a);
p.push_back(b);
p.push_back(c);
p.push_back(d);
p.push_back(e);
p.push_back(f);
p.push_back(g);
p.push_back(i);
p.push_back(j);
p.push_back(k);
v.push_back(p);
//printf("%d %d %d %d %d %d %d %d %d %d", a, b, c, d, e, f, g, i, j, k);
//puts("");
}
}
}
}
}
}
}
}
}
}
}
printf("%d\n", cnt);
for (auto i : v) {
for (auto j : i) printf("%d ", j);
puts("");
}
return 0;
}
标签:cnt,puts,int,back,烤鸡,P2089,printf,push
From: https://www.cnblogs.com/pangyou3s/p/18107751