{"title":"海运仓库货物进出统计:使用栈实现先进后出规则和最大重量查询","description":"本文介绍了如何使用 C++ 栈数据结构来模拟海运仓库的货物进出操作,并实现对当前仓库中最大集装箱重量的查询功能。","keywords":"海运仓库, 货物进出, 栈, 数据结构, C++, 最大重量, 查询","content":""#include \n#include \n\nint main() {\n int N;\n std::cin >> N;\n\n std::stack weights;\n std::stack maxWeights;\n\n for (int i = 0; i < N; i++) {\n int operation;\n std::cin >> operation;\n\n if (operation == 0) {\n int weight;\n std::cin >> weight;\n weights.push(weight);\n\n if (maxWeights.empty() || weight >= maxWeights.top()) {\n maxWeights.push(weight);\n } else {\n maxWeights.push(maxWeights.top());\n } \n } else if (operation == 1) {\n if (!weights.empty()) {\n weights.pop();\n maxWeights.pop();\n }\n } else if (operation == 2) {\n if (!maxWeights.empty()) {\n std::cout << maxWeights.top() << std::endl;\n } else {\n std::cout << 0 << std::endl;\n }\n }\n }\n\n return 0;\n}\n"输入:\n13\n0 1\n0 2\n2\n0 4\n0 2\n2\n1\n2\n1\n1\n2\n1\n2\n\n输出:\n2\n4\n4\n1\n0


原文地址: https://www.cveoy.top/t/topic/pP0t 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录