C/C++知识点之MANAGER POJ1281 C语言
小标 2018-10-10 来源 : 阅读 1512 评论 0

摘要:本文主要向大家介绍了C/C++知识点之MANAGER POJ1281 C语言,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

本文主要向大家介绍了C/C++知识点之MANAGER POJ1281 C语言,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

 One of the programming paradigm in parallel processing is the producer/consumer paradigm that can be implemented using a system with a "manager" process and several "client" processes. The clients can be producers, consumers, etc. The manager keeps a trace of client processes. Each process is identified by its cost that is a strictly positive integer in the range 1 .. 10000. The number of processes with the same cost cannot exceed 10000. The queue is managed according to three types of requests, as follows: 


 a x - add to the queue the process with the cost x; 
 r - remove a process, if possible, from the queue according to the current manager policy; 
 p i - enforce the policy i of the manager, where i is 1 or 2. The default manager policy is 1 
 e - ends the list of requests. 

       

 
       There are two manager policies: 
       

 1 - remove the minimum cost process 
 2 - remove the maximum cost process 

       

 
       The manager will print the cost of a removed process only if the ordinal number of the removed process is in the removal list. 
        
       Your job is to write a program that simulates the manager process. 
       
      

Input

       The input is from the standard input. Each data set in the input has the following format: 
       

 the maximum cost of the processes 
 the length of the removal list 
 the removal list - the list of ordinal numbers of the removed processes that will be displayed; for example 1 4 means that the cost of the first and fourth removed processes will be displayed 
 the list of requests each on a separate line. 

       

 
       Each data set ends with an e request. The data sets are separated by empty lines.
      

Output

       The program prints on standard output the cost of each process that is removed, provided that the ordinal number of the remove request is in the list and the queue is not empty at that moment. If the queue is empty the program prints -1. The results are printed on separate lines. An empty line separates the results of different data sets. 
        
       An example is given in the following:
      

Sample Input
5
2
1 3
a 2
a 3
r
a 4
p 2
r
a 5
r
e
Sample Output
2
5
 
 
 
 
 
 
 
 
 
 
我不管怎么调都不能在UVALive、ZOJ上通过,UVALive是WA,ZOJ是PE,我估计是输出格式的问题,但我已经用完我的想象力了实在不知道该怎么输出,英语也不怎么好,不怎么读的懂。
这道题就是模拟,因为不知道会有多少个数据输进来,所以我存储移除列表和进程的数组都是用的动态分配内存,用完后在释放,然后在重新申请。存储进程的数组的大小可以根据进程的最大开销来分配,存储移除列表的数组根据题目给的数分配。

 1 //removal list是输出第i个被移除的进程
 2 //如1、3,输出第1个和第3个被移除的进程 
 3 #include 
 4 #include 
 5 #include 
 6 
 7 int maxcost;//the maximum cost of the processes
 8 int p=1;//policy
 9 int rlength;//the length of the process
10 int *rlist=NULL;//用来存储移除列表的数据
11 char op;//操作
12 int *pro=NULL;//进程数组
13 int j,k=0;//j为第几次移除,k为是否成功移除的标志,0为否,1为是 
14 
15 void re(){
16     int i,a;
17     if(p==1){
18         for(i=0;i<maxcost;i++){
19             if(pro[i]!=0){//队列非空
20                 a=pro[i];
21                 pro[i]=0;
22                 j++;
23                 k=1;
24             }
25         }
26     }
27     else{
28         for(i=maxcost-1;i>=0;i--){
29             if(pro[i]!=0){//队列非空
30                 a=pro[i];
31                 pro[i]=0;
32                 j++;
33                 k=1;
34             }
35         }
36     }
37     if(k==0) printf("-1\n");//空队列输出-1
38     else{
39         for(i=0;i<rlength;i++){//在移除队列中查找有无当前的j值,有则输出,并将k置0 
40             if(rlist[i]==j){
41                 printf("%d\n",a);
42                 k=0;
43             }
44         }
45     }   
46 }
47 
48 int main(void){
49     int i;
50     while(scanf("%d",&maxcost)!=EOF){
51         j=0;//初始化移除次数为0 
52         pro=(int*)malloc(maxcost*sizeof(int));//分配存储进程的数组需要的空间 
53         memset(pro,0,maxcost*sizeof(int));
54         scanf("%d",&rlength);
55         rlist=(int*)malloc(rlength*sizeof(int));//分配存储列表的数组需要的空间
56         if(rlist!=NULL){ 
57         for(i=0;i<rlength;i++) scanf("%d",&rlist[i]);//读入removal ist
58         }
59         //开始操作
60         while(1){
61             scanf("%c",&op);scanf("%c",&op);//读取操作符
62             if(op==‘a‘){
63                 scanf("%d",&i);
64                 pro[i-1]=i;
65             }else if(op==‘r‘){
66                 re();//移除并输出
67             }else if(op==‘p‘){
68                 scanf("%d",&p);//更换policy 
69             }else if(op==‘e‘){//退出 
70                 break;
71             }else break;
72         }
73         printf("\n");
74         //释放空间 
75         free(rlist);
76         free(pro);
77     }
78     return 0;
79 }

View Code

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved