天平砝码摆放问题

题目

一个天平上有6个位置,左右各三个位置,有6个砝码,分别是1、2、3、4、5、6克重。
要使天平平衡,有多少种方法?(对称摆放算作一种方法)

image.png

题解

暴力求解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int t = 6;
int count = 0;
vector<string> v;
for (int i = 1; i <= t; ++i) {
for (int j = 1; j <= t; ++j) {
for (int k = 1; k <= t; ++k) {
for (int l = 1; l <= t; ++l) {
for (int m = 1; m <= t; ++m) {
for (int n = 1; n <= t; ++n) {
if (i == j ||
i == k || j == k ||
i == l || j == l || k == l ||
i == m || j == m || k == m || l == m ||
i == n || j == n || k == n || l == n || m == n)
continue;
if ((3 * i + 2 * j + k) == (l + 2 * m + 3 * n)) {
string s = to_string(i) + to_string(j) + to_string(k) + to_string(l) + to_string(m) +
to_string(n);
int te = 0;
if (v.size() > 0) {
string tem = s;
reverse(tem.begin(), tem.end());
vector<string>::iterator it;
for (it = v.begin(); it != v.end(); it++) {
if (*it == tem) {
//cout<<tem<<endl;
te = 1;
break;
}
}
}
if (te == 0) {
v.push_back(s);
count++;
printf(" %d %d %d | %d %d %d\n", i, j, k, l, m, n);
}
}
}
}
}
}
}
}
cout << count << endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1 4 6 | 5 3 2
1 5 6 | 2 4 3
1 5 6 | 3 2 4
1 6 4 | 3 5 2
1 6 5 | 2 3 4
2 4 6 | 1 5 3
2 4 6 | 3 1 5
2 6 3 | 4 1 5
2 6 4 | 1 3 5
3 2 6 | 5 1 4
3 4 5 | 2 1 6
3 5 4 | 1 2 6
3 6 2 | 1 5 4
4 2 6 | 1 3 5
4 3 5 | 1 2 6
5 2 4 | 3 1 6
5 4 2 | 1 3 6
17
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×