参考程序代码:
#include <iostream>
#include <vector>int main() {// 小球的重量std::vector<int> weights = {1, 2, 3, 4, 5};// 用来存储可能的结果int a, b, c, d, e, x;// 穷举所有可能的 a, b, c, d, e 的组合for (int i = 0; i < weights.size(); ++i) {for (int j = 0; j < weights.size(); ++j) {if (j == i) continue; // 确保不同的小球for (int k = 0; k < weights.size(); ++k) {if (k == i || k == j) continue;for (int l = 0; l < weights.size(); ++l) {if (l == i || l == j || l == k) continue;for (int m = 0; m < weights.size(); ++m) {if (m == i || m == j || m == k || m == l) continue;// 分别赋值a = weights[i];b = weights[j];c = weights[k];d = weights[l];e = weights[m];// 计算 x 的值// x + a = b + c ==> x = b + c - ax = b + c - a;// 验证 x 是否有效并且在范围内if (x > 0 ) {// 验证其他方程if (x + b == a + c + d + e && x + d == c + e) {// 输出结果std::cout << "a: " << a << ", b: " << b << ", c: " << c << ", d: " << d << ", e: " << e << ", x: " << x << std::endl;}}}}}}}return 0;
}