题目链接:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=838&page=show_problem&problem=146
转载自:
http://blog.csdn.net/alpc_paul/article/details/38062361#
代码如下:
#include<cstdio>
#include<queue>
#include<cstring>
#include<cstdlib>
#include<cctype>
#define maxn 10000
using namespace std;
int ip[maxn];
char prog[maxn][10];
bool flag;///标志变量
int ration;///配额
int var[26];///
///每个程序的运行时间
int c[5];
int n;///程序个数
///队列的申请方式
queue <int>BlockQ;///阻止队列
deque <int>ReadQ;///等待队列
void run(int cur)
{
int t=ration;
while(t>0)
{
char*p=prog[ip[cur]];
switch(p[2])
{
case '=':
///=
///p[4]-'0':将char型数据转换成相应的数值
var[p[0]-'a']=isdigit(p[5])?(p[4]-'0')*10+p[5]-'0':p[4]-'0';
t-=c[0];
break;
case 'i':
///print
printf("%d: %d\n",cur+1,var[p[6]-'a']);
t-=c[1];
break;
case 'c':
///lock
if(flag)
{
BlockQ.push(cur);
return ;
}
flag=true;
t-=c[2];
break;
case 'l':
flag=false;
///注意这种空操作判断,千万不要判错了
if(!BlockQ.empty())
{
int s=BlockQ.front();
BlockQ.pop();
ReadQ.push_front(s);
}
t-=c[3];
break;
case 'd':
///end
return;
}
ip[cur]++;
}
ReadQ.push_back(cur);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
///注意此处的输入语句里面有一个换行符
scanf("%d%d%d%d%d%d%d\n",&n,&c[0],&c[1],&c[2],&c[3],&c[4],&ration);
memset(var,0,sizeof(var));
int line=0;
///在进行字符串数据的输入时,一定要注意换行符
for(int i=0;i<n;i++)
{
fgets(prog[line++],maxn,stdin) ;
ip[i]=line-1;
while(prog[line-1][2]!='d')
fgets(prog[line++],maxn,stdin) ;
ReadQ.push_back(i);
}
flag=false;
while(!ReadQ.empty())
{
int cur=ReadQ.front();
ReadQ.pop_front();
run(cur);
}
if(T)
printf("\n");
}
return 0;
}