哎,感觉考试好难啊啊啊啊啊啊!!!!40个选择题+两个编程题。120分钟。那么多读程序的题目。题目都看不完有木有啊!!!!!!!!感觉整个人生都不好了。
题目描述:(具体的也记不清楚了,但是主要内容就是下面的)
模拟内存的分配 释放 和 整理。
操作如下:
new size:分配size字节大小的内存块,返回内存块的句柄handle,size为正整数,如果没有合适的空间返回null。
del handle:释放句柄handle指向的内存块。
def 整理内存碎片。
输入第一行为一个n,m , 分别表示操作次数和内存空间大小。
接下来n行是表示一个操作。
样例:
输入:
6 10
new 5
new 3
del 1
new 6
def
new 6
输出:
1
2
NULL
3
1 #include<iostream> 2 #include<list> 3 #include<string> 4 using namespace std; 5 struct Memory{ 6 unsigned int value; 7 unsigned handle ; 8 bool isuesed;//使用1 没有使用0 9 }; 10 11 void New(list<Memory> &list_memory,unsigned int value,int &handle ){ 12 bool is_success = false; 13 for (list<Memory>::iterator i = list_memory.begin(); i != list_memory.end(); i++){ 14 if (!(*i).isuesed && (*i).value >= value){ 15 if ((*i).value == value){ 16 (*i).isuesed = 1; 17 (*i).handle = handle++; 18 } 19 else{ 20 Memory m; 21 m.handle = handle++; 22 m.isuesed = 1; 23 m.value = value; 24 list_memory.insert(i, m); 25 (*i).value -= value; 26 } 27 is_success = true; 28 cout << handle - 1 << endl; 29 break; 30 } 31 } 32 if (!is_success){ 33 cout << "NULL" << endl; 34 } 35 } 36 37 void Def(list<Memory> &list_memory){ 38 int value = 0; 39 for (list<Memory>::iterator i = list_memory.begin(); i != list_memory.end();){ 40 if (!(*i).isuesed){ 41 value += (*i).value; 42 (*i).handle = 0; 43 i = list_memory.erase(i); 44 45 } 46 else{ 47 i++; 48 } 49 50 } 51 if (value){ 52 Memory me; 53 me.isuesed = 0; 54 me.value = value; 55 me.handle = 0; 56 list_memory.push_back(me); 57 } 58 } 59 60 void Delete(list<Memory> &list_memory,unsigned int value){ 61 bool is_success = false; 62 for (list<Memory>::iterator i = list_memory.begin(); i != list_memory.end(); i++){ 63 64 if ((*i).handle == value){ 65 (*i).isuesed = 0; 66 (*i).handle = 0; 67 int _del_value; 68 if (i != list_memory.begin()){ 69 i--; 70 if (!(*i).isuesed){ 71 72 _del_value = (*i).value; 73 i = list_memory.erase(i); 74 (*i).value += _del_value; 75 } 76 else{ 77 i++; 78 } 79 } 80 i++; 81 if (i != list_memory.end() && !(*i).isuesed){ 82 _del_value = (*i).value; 83 i = list_memory.erase(i); 84 i--; 85 (*i).value += _del_value; 86 } 87 is_success = true; 88 break; 89 } 90 } 91 if (!is_success){ 92 cout << "error" << endl; 93 } 94 } 95 96 int main(){ 97 int n, m; 98 while (cin >> n >> m){ 99 list<Memory> list_memory; 100 Memory me; 101 me.value = m; 102 me.isuesed = 0; 103 me.handle = 0; 104 list_memory.push_back(me); 105 static int handle = 1; 106 for (int l = 0; l < n; l++){ 107 string op; 108 unsigned int value; 109 cin >> op; 110 if (op == "new"){ 111 cin >> value; 112 New(list_memory, value,handle); 113 } 114 else if (op == "def"){ 115 116 Def(list_memory); 117 } 118 else if (op == "del"){ 119 cin >> value; 120 Delete(list_memory, value); 121 } 122 123 for (list<Memory>::iterator i = list_memory.begin(); i != list_memory.end(); i++){ 124 cout << (*i).value << ‘ ‘ << (*i).isuesed << ‘ ‘ << (*i).handle << endl; 125 } 126 cout << "---------------------分割线-------------------------------------------" << endl; 127 } 128 } 129 } 130
时间: 2024-10-29 01:14:21