参考程序代码:
#include <iostream>
#include <vector>
#include <algorithm>using namespace std;int main() {// 各种颜色宝石的数量vector<int> gems = {11, 22, 33, 44, 55, 66, 77};int totalBoxes = 0;while (true) {// 对宝石数量进行排序,从大到小sort(gems.rbegin(), gems.rend());// 检查是否至少有5种颜色的宝石可用int count = 0;for (int i = 0; i < 7; ++i) {if (gems[i] > 0) {count++;}}if (count < 5) {break; // 不能再装礼盒,退出循环}// 放入一个礼盒for (int i = 0; i < 5; ++i) {if (gems[i] > 0) {gems[i]--; // 每种颜色的宝石数量减1}}totalBoxes++; // 礼盒数量加1}cout << "最多可以装的礼盒数量: " << totalBoxes << endl;return 0;
}