标签:
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
标签:
原文地址:http://www.cnblogs.com/muqidi/p/5861436.html