码迷,mamicode.com
首页 > 其他好文 > 详细

队列的典型实例问题——CPU时间片轮转调度

时间:2019-06-07 19:30:12      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:nbsp   str   namespace   std   turn   cpu   color   main   cin   

 1 //eg输入5 100
 2 //      p1 150
 3 //      p2 80
 4 //      p3 200
 5 //      p4 350
 6 //      p5 20
 7 #include<iostream>
 8 #include<string>
 9 #include<queue>
10 using namespace std;
11 int main(){
12     int n;//任务数
13     int time;//时间片
14     cin>>n; 
15     cin>>time;
16     string p[100000];//任务名
17     int t[100000]; 
18     string pp;
19     queue<string>q;
20     for(int i=1;i<=n;i++){//输入各个任务和用时 
21         cin>>p[i];
22         q.push(p[i]);
23         cin>>t[i];
24     } 
25     int timeans=0;//计时器 
26     while(!q.empty()){
27         pp=q.front();    
28         if(t[pp[1]-0]<=time){//耗时小于等于时间片
29             timeans=timeans+t[pp[1]-0];//计时器加上这次的时间 
30             q.pop();//完成后出队 
31             cout<<pp<< <<timeans<<endl; 
32         }
33         else{//耗时大于时间片
34             timeans=timeans+time;//计时器加上这次的时间
35             t[pp[1]-0]-=time;//更新这个任务的用时            
36             q.pop();//出队
37             q.push(pp); 
38         }
39     }
40     return 0;
41 } 

 

队列的典型实例问题——CPU时间片轮转调度

标签:nbsp   str   namespace   std   turn   cpu   color   main   cin   

原文地址:https://www.cnblogs.com/TYXmax/p/10988817.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!