rt,找了好久都没debug到问题,下面是我的代码
#include<bits/stdc++.h>
using namespace std;
queue<int> stopq;//阻止队列
deque<int> stdq;//标准队列
vector<string> cmd[1001];
//p相当于光标,p[pid]表示第pid个程序目前进行的行数
int n,times[5],q,p[1001];
map<char,string> pdata;//公共变量域:变量名->值(想试试用map做题,本来是用数组来存变量的)
string para;
bool locked;
void run(int pid){
int t=q,v;//这里的t是指给每个程序分配的时间(即输入的Q)
while(t>0){
string cur=cmd[pid][p[pid]];
switch(cur[2]){
case '=':{//x=y
t-=times[0];
pdata[cur[0]]=cur.substr(4);
break;
}
case 'i':{//print
t-=times[1];
cout<<pid<<": "<<pdata[cur[6]]<<endl;
break;
}
case 'c':{//lock
t-=times[2];
if(locked){
stopq.push(pid);
return;
}else{
locked=1;
}
break;
}
case 'l':{//unlock
t-=times[3];
locked=0;
if(!stopq.empty()){
int id=stopq.front();
stopq.pop();
stdq.push_front(id);
}
break;
}
case 'd':{//end
t-=times[4];
return;
}
}
p[pid]++;
}
stdq.push_back(pid);
}
int main(){
int t;
cin>>t;
while(t--){
cin>>n;
for(int i=0;i<5;i++) cin>>times[i];
cin>>q;
for(int pid=1;pid<=n;pid++){
cmd[pid].clear();
while(getline(cin,para)){
cmd[pid].push_back(para);
if(cmd[pid].back()=="end") break;
}
stdq.push_back(pid);
}
memset(p,0,sizeof(p));
locked=0;
while(!stdq.empty()){
int pid=stdq.front();
stdq.pop_front();
run(pid);//先pop后再run
}
if(t) cout<<endl;
}
return 0;
}