1 条题解
-
0
题目分析
题目要求模拟电路保险丝的工作状态。给定n个电器设备(每个设备有额定电流值ci)、m次操作(每次操作切换某个设备的状态)和保险丝容量c。需要判断在m次操作过程中,电路总电流是否会超过保险丝容量c,并输出最大电流值。
解题思路
- 输入处理:读取设备数量n、操作次数m和保险丝容量c。
- 状态跟踪:使用数组
flag
记录每个设备的开关状态(0关闭,1开启)。 - 电流计算:
- 每次操作切换设备状态时,更新总电流
ci_total
。 - 记录过程中的最大电流
max
。
- 每次操作切换设备状态时,更新总电流
- 结果判断:比较
max
和c,输出保险丝是否熔断及最大电流值。
代码实现
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n, m, c; int caseNum = 1; while (cin >> n >> m >> c && (n != 0 || m != 0 || c != 0)) { vector<int> powers(n); for (int i = 0; i < n; i++) { cin >> powers[i]; } vector<bool> isOn(n, false); // 记录电器开关状态,初始全关闭 int currentPower = 0; // 当前总功率 int maxPower = 0; // 最大功率 bool fuseBlown = false; // 保险丝是否烧断 for (int i = 0; i < m; i++) { int device; cin >> device; device--; // 转换为0-based索引 // 切换电器状态 if (isOn[device]) { currentPower -= powers[device]; } else { currentPower += powers[device]; } isOn[device] = !isOn[device]; // 更新最大功率 maxPower = max(maxPower, currentPower); // 检查保险丝是否烧断 if (currentPower > c) { fuseBlown = true; } } // 输出结果 cout << "Sequence " << caseNum++ << endl; if (fuseBlown) { cout << "Fuse was blown." << endl; } else { cout << "Fuse was not blown." << endl; cout << "Maximal power consumption was " << maxPower << " amperes." << endl; } cout << endl; // 每个测试用例后空行 } return 0; }
- 1
信息
- ID
- 485
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 2
- 已通过
- 1
- 上传者