酷代码 AI
菜单
服务商

C++实现能量发射器与水晶最优配对,计算最多激活水晶数量

```cpp #include <iostream> #include <algorithm> #include <vector> int main() { int N; // 输入发射器和水晶的数量 std::cin >> N; std::vector<int> emitters(N); std::vector<int> crystals(N); // 输入每个能量发射器的能量值 for (int i = 0; i < N; ++i) { std::cin >> emitters[i]; } // 输入每块水晶的能量需求值 for (int i = 0; i < N; ++i) { std::cin >> crystals[i]; } // 对发射器的能量值和水晶的能量需求值进行排序 std::sort(emitters.begin(), emitters.end()); std::sort(crystals.begin(), crystals.end()); int emitterIndex = 0; int crystalIndex = 0; int activatedCrystals = 0; // 遍历发射器和水晶,尝试进行匹配 while (emitterIndex < N && crystalIndex < N) { if (emitters[emitterIndex] >= crystals[crystalIndex]) { // 当发射器的能量值大于或等于水晶的能量需求值时,激活该水晶 ++activatedCrystals; ++emitterIndex; ++crystalIndex; } else { // 若发射器能量值不足,尝试下一个发射器 ++emitterIndex; } } // 输出最多能成功激活的水晶数量 std::cout << activatedCrystals << std::endl; return 0; } ``` ######[AI问答 | 401点数解答 | 2026-03-08 10:25:27]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]