题意:n个程序,每个程序包含不超过25条语句,每个程序有执行时间,时间片到进入等待队列,下一个继续,当一个程序执行lock语句后,再有程序执行lock会放入阻止队列,直到那个程序执行完unlock语句后,阻止队列才一个程序出队,但是这个程序要插入等待队列的首部!执行到print语句输出相应的值,直到全部程序完成!
思路:用vector存放程序语句,用deque作为等待队列,用queue作为阻止队列,用一个一维数组记录每次每个程序执行到哪个位置!deque和queue中只需存放程序编号即可!
deque 尾部入队:push_back();首部入队:push_front();
尾部出队:pop_back();首部出队:pop_front();
清空队列:clear();
......
参考:入门经典--例6-1--P139
代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <map>
#include <deque>
#include <string.h>
using namespace std;
deque<int>team;
queue<int>preventNum;
vector<string>statement[1024];//存放程序语句
int teamNumber,flag,t[6],q,record,cnt[1024],var[26];
void choose(string state,int& times)
{
switch(state[2])
{
case '=':
times -= t[1];
var[state[0]-'a'] = state.length() == 5 ? state[4] - '0' : (state[4] - '0')*10+(state[5] - '0');
break;
case 'i':
times -= t[2];
printf("%d: %d\n",teamNumber,var[state[6] - 'a']);
break;
case 'c':
times -= t[3];
if(flag)//是否有已存在lock
{
preventNum.push(teamNumber);
record = 0;
times = 0;
}
flag = 1;//标记存在lock
break;
case 'l':
times -= t[4];
if(!preventNum.empty())//判断阻止队列是否有程序
{
team.push_front(preventNum.front());//插入首部
preventNum.pop();
}
flag = 0;//lock已结束
break;
case 'd':
times = 0;
break;
}
}
void simulation()
{
string state;
while(!team.empty())
{
record = 1;
teamNumber = team.front();
team.pop_front();
int times = q;
while(times > 0)
{
int i = teamNumber;
state = statement[i][cnt[i]];
choose(state,times);
if(record)cnt[i]++;//关键:用一个数组记录每个程序当前执行到哪!
}
if(state != "end" && record)
team.push_back(teamNumber);
}
}
int main()
{
int n,test;
string str;
cin >> test;
while(test--)
{
cin >> n;
for(int i=1;i<=5;i++)
cin >> t[i];
cin >> q;
getchar();
int cot = 0;
while(getline(cin,str))
{
statement[cot+1].push_back(str);
if(str == "end")
{
cot++;
team.push_back(cot);
}
if(cot == n)
break;
}
//初始化
memset(var,0,sizeof(var));
memset(cnt,0,sizeof(cnt));
simulation();
//清空容器
team.clear();
while(!preventNum.empty()) preventNum.pop();
for(int i=1;i<=n;i++)
statement[i].clear();
if(test)cout << endl;
}
return 0;
}
这个是将每个程序语句也装入了队列,然后等待队列里直接存放的是每个程序的队列,这个写的比较啰嗦!
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <map>
#include <deque>
using namespace std;
queue<string>temp,part;
deque< queue<string> >que;
queue< queue<string> >prevent;
deque<int>team;
queue<int>preventNum;
map<char,int>var;//用于存放变量
int teamNumber,flag,t[6],q,record;
void choose(string state,int& times)
{
switch(state[2])
{
case '=':
times -= t[1];
var[state[0]] = state[5] == '\0' ? state[4] - '0' : (state[4] - '0')*10+(state[5] - '0');
break;
case 'i':
times -= t[2];
if(var.count(state[6]))
printf("%d: %d\n",teamNumber,var[state[6]]);
else
printf("%d: 0\n",teamNumber);
break;
case 'c':
times -= t[3];
if(flag)
{
prevent.push(temp);
preventNum.push(teamNumber);
record = 0;
times = 0;//进入阻止队列时间清零
}
flag = 1;
break;
case 'l':
times -= t[4];
if(!prevent.empty())
{
que.push_front(prevent.front());
team.push_front(preventNum.front());
prevent.pop();
preventNum.pop();
}
flag = 0;
break;
case 'd':
times = 0;//结束时间清零
break;
}
}
void simulation()
{
string state;
while(!que.empty())
{
record = 1;
temp = que.front();
teamNumber = team.front();
que.pop_front();
team.pop_front();
int times = q;
while(times > 0)
{
state = temp.front();
//cout << state << endl;
choose(state,times);
temp.pop();
}
if(state != "end" && record)
{
que.push_back(temp);
team.push_back(teamNumber);
}
}
}
int main()
{
int n,test;
string str;
cin >> test;
while(test--)
{
cin >> n;
for(int i=1;i<=5;i++)
cin >> t[i];
cin >> q;
getchar();
int cot = 0;
while(getline(cin,str))
{
part.push(str);
if(str == "end")
{
cot++;
que.push_back(part);
team.push_back(cot);
while(!part.empty()) part.pop();
}
if(cot == n)
break;
}
simulation();
que.clear();
while(!temp.empty()) temp.pop();
while(!part.empty()) part.pop();
while(!prevent.empty()) prevent.pop();
team.clear();
while(!preventNum.empty()) preventNum.pop();
var.clear();
if(test)cout << endl;
}
return 0;
}