UVA 210 - Concurrency Simulator

简单题,利用双端队列来模拟普通的执行队列(因为可能需要在队列头部进行插入),利用普通队列来模拟阻塞队列。题目中的样例输入是个坑:应该先给出Case的个数,再给出各个测试数据,但是题目中的样例没有给出Case的个数,仅仅是直接给出了一组测试数据,大家在解题的时候要小心一点。

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<iomanip>
using namespace std;

typedef struct prog{
	int cur_pos;
	vector<string> instruction;
}prog;

int amount, Ass_t, Out_t, Beg_t, End_t, Stop_t,Exc_t;

int main(){
	int Case = 0;
	cin >> Case;
		while (Case){
			vector<int> value(26, 0);
			cin >> amount >> Ass_t >> Out_t >> Beg_t >> End_t >> Stop_t >> Exc_t;
			string eat;
			getline(cin, eat);
			vector<prog> program;
			deque<int> Excute;
			queue<int> Block;
			bool flag_l = false;
			for (int i = 0; i < amount; i++){
				prog temp;
				temp.cur_pos = 0;
				string ins;
				while (getline(cin, ins)){
					string del = "";
					for (int i = 0; i < ins.size(); i++){//去除所有的空格
						if (ins[i] != ' ') del += ins[i];
					}
					ins = del;
					temp.instruction.push_back(ins);
					if (ins == "end") break;
				}
				program.push_back(temp);
				Excute.push_back(i);
			}
			int finish = 0;//finish表示完成的程序数量
			while (finish < amount){
				int index = Excute.front();
				Excute.pop_front();
				int use_time = Exc_t;//分配时间片
				bool is_in = true;
				while (use_time > 0){
					if (program[index].cur_pos >= program[index].instruction.size()) break;
					string instruct = program[index].instruction[program[index].cur_pos];//取指令
					if (instruct.find("print") != -1){//print
						int position = instruct[instruct.size() - 1] - 'a';
						cout << (index + 1) << ": " << value[position] << endl;
						use_time -= Out_t;
						program[index].cur_pos++;
					}
					else if (instruct == "lock"){
						if (flag_l == true){
							use_time = 0;
							Block.push(index);
							is_in = false;
						}
						else{//flag_l=false
							flag_l = true;
							use_time -= Beg_t;
							program[index].cur_pos++;
						}
					}
					else if (instruct == "unlock"){
						flag_l = false;
						use_time -= End_t;
						program[index].cur_pos++;
						if (!Block.empty()){
							int t = Block.front();
							Block.pop();
							Excute.push_front(t);
						}
					}
					else if (instruct == "end"){
						finish++;
						use_time = 0;
						is_in = false;
					}
					else{//"="
						int position = instruct[0] - 'a';
						int res = 0;
						for (int k = 2; k < instruct.size(); k++){
							res = res * 10 + (instruct[k] - '0');
						}
						value[position] = res;
						use_time -= Ass_t;
						program[index].cur_pos++;
					}
				}
				if (program[index].cur_pos == program[index].instruction.size()) is_in = false;
				if (is_in) Excute.push_back(index);
			}
			Case--;
		if (Case) cout << endl;
		}
		//system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值