以下是一个可能的 C++ 解决方案: ```cpp #include <iostream> // 计算得分的函数 int calculateScore(int x, int a1, int a2, int a3) { if (x < a1) { return 0; } else if (x >= a1 && x < a2) { return 30; } else if (x >= a2 && x < a3) { return 60; } else { return 100; } } int main() { int a1, a2, a3; int b1, b2, b3; int c1, c2, c3; int d1, d2, d3; std::cin >> a1 >> a2 >> a3; std::cin >> b1 >> b2 >> b3; std::cin >> c1 >> c2 >> c3; std::cin >> d1 >> d2 >> d3; int maxScore = 0; int bestX1 = 0, bestX2 = 0, bestX3 = 0, bestX4 = 0; for (int x1 = 0; x1 <= 120; x1++) { for (int x2 = 0; x2 <= 120 - x1; x2++) { for (int x3 = 0; x3 <= 120 - x1 - x2; x3++) { int x4 = 120 - x1 - x2 - x3; int score1 = calculateScore(x1, a1, a2, a3); int score2 = calculateScore(x2, b1, b2, b3); int score3 = calculateScore(x3, c1, c2, c3); int score4 = calculateScore(x4, d1, d2, d3); int totalScore = score1 + score2 + score3 + score4; if (totalScore > maxScore) { maxScore = totalScore; bestX1 = x1; bestX2 = x2; bestX3 = x3; bestX4 = x4; } } } } std::cout << bestX1 << " " << bestX2 << " " << bestX3 << " " << bestX4 << std::endl; return 0; } ``` 以下是三个改进建议: 1. 可以添加一些边界检查和错误处理代码,以处理输入不符合要求的情况。 2. 考虑使用更高效的算法来优化搜索过程,例如分支定界法。 3. 对代码进行模块化,将计算得分和搜索最佳方案的部分分别封装成独立的函数,以提高代码的可读性和可维护性。 [2024-12-27 15:38:55 | AI写代码神器 | 673点数解答]