#include <iostream> #include <vector> #include <string> using namespace std; // 初始化位示图 vector<int> bitMap(64, 0); // 进程结构体 struct Process { int processNum; int pageRequested; }; // 判断是否有足够的内存空间 bool checkSpace(int numPages) { int count = 0; for (int i = 0; i < 64; i++) { if (bitMap[i] == 0) { count++; } if (count == numPages) { return true; } } return false; } // 分配内存空间给进程 int allocateMemory(Process process) { int numPages = process.pageRequested; if (checkSpace(numPages)) { int startIndex = 0; for (int i = 0; i < numPages; i++) { while (bitMap[startIndex] != 0) { startIndex++; } bitMap[startIndex] = process.processNum; } return startIndex; } else { return -1; } } int main() { vector<Process> processes; for (int i = 0; i < 4; i++) { int pageRequested; cin >> pageRequested; Process newProcess = {i+1, pageRequested}; processes.push_back(newProcess); } int processNum, pageNum; cin >> processNum >> pageNum; for (Process process : processes) { if (process.processNum == processNum) { int result = allocateMemory(process); if (result != -1) { if (pageNum >= 0 && pageNum < process.pageRequested) { cout << result << endl; } else { cout << "error" << endl; } } else { cout << "wait" << endl; } break; } } return 0; } [2024-11-21 15:45:29 | AI写代码神器 | 1154点数解答]