"Qihoo 360" 2016-year technical gang a programming problem--memory management

Source: Internet
Author: User

Ah, feel the exam is very difficult ah ah ah Ah!!!! 40 Choice questions + two programming questions. 120 minutes. So much to read the subject of the program. There's no end to all the questions.!!!!!!!! It feels like the whole life is bad. Title Description: (The specific can not remember clearly, but the main content is the following) simulation of the allocation of memory release and collation. The operation is as follows: New size: Allocates a memory block of size bytes, returns a handle to the memory block Handle,size is a positive integer, and returns null if there is no appropriate space. Del handle: Releases the memory block that the handle handle points to. def collates memory fragments. Enter the first behavior of a n,m, representing the number of operations and the size of the memory space, respectively. The next n rows represent an operation. Example: Input: 6 10new 5new 3del 1new 6defnew 6 output: 12null3
1#include <iostream>2#include <list>3#include <string>4 using namespacestd;5 structmemory{6Unsignedintvalue;7 unsigned handle;8     BOOLisuesed;//use 1 not used 09 };Ten  One voidNew (list<memory> &list_memory,unsignedintValueint&handle) { A     BOOLIs_success =false; -      for(List<memory>::iterator i = List_memory.begin (); I! = List_memory.end (); i++){ -         if(! (*i). isuesed && (*i). Value >=value) { the             if((*i). Value = =value) { -(*i). isuesed =1; -(*i). Handle = handle++; -             } +             Else{ - Memory m; +M.handle = handle++; Am.isuesed =1; atM.value =value; - List_memory.insert (i, m); -(*i). Value-=value; -             } -Is_success =true; -cout << Handle-1<<Endl; in              Break; -         } to     } +     if(!is_success) { -cout <<"NULL"<<Endl; the     } * } $ Panax Notoginseng voidDef (List<memory> &list_memory) { -     intValue =0; the      for(List<memory>::iterator i = List_memory.begin (); I! =list_memory.end ();) { +         if(! (*i). isuesed) { AValue + = (*i). value; the(*i). Handle =0; +i =list_memory.erase (i); -  $         } $         Else{ -i++; -         } the  -     }Wuyi     if(value) { the Memory me; -me.isuesed =0; WuMe.Value =value; -Me.handle =0; About List_memory.push_back (me); $     } - } -  - voidDelete (list<memory> &list_memory,unsignedintvalue) { A     BOOLIs_success =false; +      for(List<memory>::iterator i = List_memory.begin (); I! = List_memory.end (); i++){ the  -         if((*i). Handle = =value) { $(*i). isuesed =0; the(*i). Handle =0; the             int_del_value; the             if(I! =List_memory.begin ()) { thei--; -                 if(! (*i). isuesed) { in                      the_del_value = (*i). value; thei =list_memory.erase (i); About(*i). Value + =_del_value; the                 } the                 Else{ thei++; +                 } -             } thei++;Bayi             if(I! = List_memory.end () &&! (*i). isuesed) { the_del_value = (*i). value; thei =list_memory.erase (i); -i--; -(*i). Value + =_del_value; the             } theIs_success =true; the              Break; the         } -     } the     if(!is_success) { thecout <<"Error"<<Endl; the     }94 } the  the intMain () { the     intN, M;98      while(Cin >> N >>m) { AboutList<memory>list_memory; - Memory me;101Me.Value =m;102me.isuesed =0;103Me.handle =0;104 List_memory.push_back (me); the         Static intHandle =1;106          for(intL =0; l < N; l++){107             stringop;108Unsignedintvalue;109CIN >>op; the             if(OP = ="New"){111CIN >>value; the New (List_memory, value,handle);113             } the             Else if(OP = ="def"){ the                  the Def (list_memory);117             }118             Else if(OP = ="del"){119CIN >>value; - Delete (list_memory, value);121             }122 123              for(List<memory>::iterator i = List_memory.begin (); I! = List_memory.end (); i++){124cout << (*i). Value <<' '<< (*i). isuesed <<' '<< (*i). Handle <<Endl; the             }126cout <<"---------------------Split Line-------------------------------------------"<<Endl;127         } -     }129 } the  

"Qihoo 360" 2016-year technical gang a programming problem--memory management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.