First question, analog cache
After understanding test instructions Direct Code code is possible. However, there is a small trick in saving the data structure selection of the cache, if you use a normal array to save, then when adding a new page, if the array is not full, then directly append to the end, if the cache is full, you need to retire the first page. One of the things you need to do when you retire an old page is to cover the second page to the last page, empty out the last position, and then place the new page in the last position. The time complexity of this elimination is cache length m. One improvement is to reserve the length of the cache directly with queue queues. The trick I'm using here is to use a few variables to refine the original normal array into a loop array pattern, as shown in the following code. Unfortunately, all test cases using the normal array pass, using the loop array can only reach 13/16, but also lack of consideration, if found, trouble criticism, thank you.
#include <iostream>using namespace Std;int countmisspage (int, int*, int); int main () {int page_requests[] = {1,2,1,3 ,};int max_cache_size = 2;int len = 6;cout << countmisspage (max_cache_size, page_requests, Len) << Endl;} int countmisspage (int max_cache_size, int* page_requests, int n) {if (!page_requests | | n <= 0) return 0;if (max_cache_s ize <= 0) return n;int Countmiss = 0;int Currcachenum = 0;//used to record the header of the current loop array int front = 0;//Cache array, page numbering all initialized to 0int* cache = New Int[max_cache_size] {0};for (int i = 0; i < n; ++i) {bool Isfind = false;//Find whether the requested page is in the current cache for (int j = 0; J < m Ax_cache_size; ++J) if (cache[j] = = Page_requests[i]) {isfind = True;break;} The requested page is no longer cached in if (!isfind) {//misses plus one countmiss++;//if the cache array is not full, directly append at the end of the array if (Currcachenum < max_cache_size) cache[ currcachenum++] = page_requests[i];//If full, the original team first placed in a new page, modify the next one for the new team first, the original team first become the end of the team else {Cache[front] = Page_requests[i];front = ( Front + 1)% Max_cache_size;}}} Delete[] Cache;return Countmiss;}
The second problem, simulation of the shortest operation first
Analysis test instructions that because the CPU does not appear idle in test instructions, that is, the CPU has finished processing the previous job, the next job time has not arrived. For example, the second test case, the P4 job time of 5 is significantly less than P3 7, but the CPU can not choose P4 first processing and let the cumulative CPU time from 3 seconds (P1 and P2 Cumulative job time) to 9 seconds (P4 to reach the time) in the middle of this 6 seconds idle do not do other tasks and only wait for P4. With this condition, the whole problem is much simpler, otherwise you need to consider if a number of not arrived at the job, should choose the job time is short, or time to arrive first, or there are other strategies, the situation becomes very complex (I just ignore the CPU will not be idle and stepping into the pit!) ), the rest, the code has comments:
#include <iostream>using namespace Std;float waitingtimesjf (int*, int*, int); int main () {int requesttime[] = {0,2,4 , 5}/*{0,1,3,9}*/;int durations[] = {7,4,1,4}/*{2,1,7,5}*/;int n = 4;cout << waitingtimesjf (RequestTime, Durat Ions, N) << Endl;} Float WAITINGTIMESJF (int* requesttime, int* durations, int n) {if (!requesttime | |!durations | | n <= 0) return 0.0f;boo l* isvisited = new Bool[n] {false};int TotalTime = durations[0];int WaitTime = 0;isvisited[0] = true;int index;for (int i = 1; I < n; ++i) {//test instructions, a single job service time is less than 100int Mindur = 100;//for the remaining n-1 jobs, do the following loop: 1) Whether it has been submitted, 2) whether the job has arrived, 3) Select the shortest job for all the jobs that have been reached for (int j = 0; J < ; N ++J) {if (!isvisited[j] && requesttime[j] <= totaltime && durations[j] < Mindur) {Mindur = durations [J];index = j;}} WaitTime + = (Totaltime-requesttime[index]); TotalTime + = Durations[index];isvisited[index] = true;} return (float) waitTime/(float) n;}
The third question, the decision Tree 2 is the tree 1 subtree
This problem forget and concrete test instructions, in general with the Sword refers to offer or other relevant information test instructions almost, the difference "not much" point is very key: In other books subtree, as long as the tree 2 of all the data and structure in the tree 1 appears, then the decision to meet the conditions, regardless of the tree 2 in the tree 1 location. But Baidu this problem, according to my test, tree 2 must be if the leaf node and tree 1 leaf node corresponds to the conditions. Such as:
Condition 1 Returns True, Condition 2 returns FALSE. So at first I use the conventional judgment whether the subtree can only past the case1 and not case2, after a slight modification of the condition after the two test cases are over, and then all the other test cases over 19 of the 17, or there are some cases are not considered comprehensive, test instructions and code are not left, Do not test it.
Baidu 2016 Summer Internship Machine questions (part)