奥数与C++小学四年级(第十八题 小球重量)
参考程序代码:
#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 - a
x = 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;
}