代码1:
#include <iostream>
using namespace std;
int main() {
int originalWeight = 0;
bool found = false;
// 设定枚举范围
const int MAX_WEIGHT = 100;
for (originalWeight = 0; originalWeight <= MAX_WEIGHT; ++originalWeight) {
int weightAfterRemoving = originalWeight - 24; // 甲堆运走24吨后的重量
int weightAfterAdding = originalWeight + 8; // 乙堆运入8吨后的重量
// 检查乙堆重量是否是甲堆重量的3倍
if (weightAfterAdding == 3 * weightAfterRemoving) {
found = true;
break;
}
}
if (found) {
cout << "两堆煤原来各有 " << originalWeight << " 吨煤。" << endl;
} else {
cout << "在给定的范围内没有找到满足条件的解。" << endl;
}
return 0;
}
代码2
#include <iostream>
using namespace std;
int main() {
double x; // 假设两堆煤原来的重量是 x 吨
// 建立方程 (x + 8) = 3 * (x - 24)
// 展开并整理方程
double left_side = x + 8;
double right_side = 3 * (x - 24);
// 解方程
x = (left_side - right_side) / (3 - 1);
x = (8 - (-72)) / 2;
x = 80 / 2;
x = 40;
// 输出结果
cout << "两堆煤原来各有 " << x << " 吨煤。" << endl;
return 0;
}