题目链接:https://vjudge.net/problem/UVA-210

题目分析

就是一道模拟题,但是细节有点多。
写代码两个小时,调试代码用了两天。。。很长时间不刷题了,这道虽然算法简单但是细节满满的题目对我来说是一个很好的热身。

  • 尽量不要去使用匿名名字空间,发现对调试过程不怎么友好(陈硕大大说的对)。
  • 使用枚举类型对程序的可读性、可维护性的提升非常大
  • 重载输入输出运算符的时候一定要记得返回stream对象
  • 这种带有switch语句的,可以使用Stragety模式,这里没有使用,因为每条语句只有执行,没有复杂的行为
  • 多组数据一定要有init函数清空数据
  • 使用智能指针不要用引用,这一点点内存的消耗不算什么,但是使用引用往往会带来错误:当我们在对象内部不小心将其释放掉的时候就会产生段错误,而且很难排查
  • 良好的抽象是巧妙设计的基础,每个类应该只负责其分内的事,不要尝试让其去做超过他权限的事,因为这样往往会让事情一团糟。封装、抽象能够帮助我们处理复杂的情况。
  • 通过ulimit -c unlimited命令开启生成core文件可以帮助进行调试
  • gdb调试开始的时候可以使用run < input.txt重定向输入和输出
  • cgdb真好用

AC代码

#include <iostream>
#include <array>
#include <vector>
#include <string>
#include <deque>
#include <memory>using namespace std;namespace {enum TYPE {ASSIGN, PRINT, LOCK, UNLOCK, END
};
int n, quantum;constexpr int MAXN = 26;
array<int, MAXN> alpha = {};bool lock = false;
class Statement {public:static constexpr int MAXN = 5;static array<int, MAXN> cost;static void init();string line;TYPE type;int var;int constant;int exec();friend istream& operator >> (istream& is, Statement &self);friend ostream& operator << (ostream& os, const Statement &self);
};class Program {public:vector<Statement> statements;int idx = 0;int id;bool exec();Program(int _id) : id(_id) {}friend istream& operator >> (istream& is, Program& self);friend ostream& operator << (ostream& os, const Program &self);
};
deque<shared_ptr<Program>> readyQueue, blockedQueue;
shared_ptr<Program> p;ostream& operator << (ostream& os, const Statement &self) {//    os << self.type;switch (self.type) {case ASSIGN:os << static_cast<char>('a' + self.var) << " = " << self.constant;break;case PRINT:os << "print " << static_cast<char>('a' + self.var);break;case LOCK:os << "lock";break;case UNLOCK:os << "unlock";break;case END:os << "end";break;}return os;
}ostream& operator << (ostream& os, const Program &self) {os << "ID:" << self.id << "\n";for (auto s : self.statements) {os << s << "\n";}os << "\n";return os;
}int Statement::exec() {switch (type) {case ASSIGN:
//        cout << "Test:" << line << endl;
//        cout << "Test:" << readyQueue.front()->id << " " << static_cast<char>('a' + var) << " = " << constant << endl;alpha[var] = constant;return cost[type];break;case PRINT:cout << p->id << ": " << alpha[var] << "\n";return cost[type];break;case END:
//        readyQueue.pop_front();
//        cout << "Test:" << type << " " << cost[type] << endl;
//        for (int i = 0; i < Statement::MAXN; ++i) {//            cout << cost[i] << " ";
//        }
//        cout << endl;return cost[type];break;case LOCK:if (lock) {blockedQueue.push_back(p);return -1;} else {lock = true;return cost[type];}break;case UNLOCK:if (!blockedQueue.empty()) {readyQueue.push_front(blockedQueue.front());blockedQueue.pop_front();}lock = false;return cost[type];default:break;}
}bool Program::exec() {int time = quantum;while (time > 0) {int ret = statements[idx].exec();if (ret == -1) {//lockreturn false;}if (++idx == statements.size()) {//endreturn false;}time -= ret;}return true;
}constexpr int Statement::MAXN;
array<int, Statement::MAXN> Statement::cost;void Statement::init() {for (int i = 0; i < MAXN; ++i) cin >> cost[i];
}istream& operator >> (istream& is, Statement &self) {auto &line = self.line;getline(is, line);if (line[1] == ' ') {self.type = ASSIGN;self.var = line[0] - 'a';self.constant = stoi(line.substr(4));} else if (line[0] == 'p') {self.type = PRINT;self.var = line[6] - 'a';} else if (line[0] == 'l') {self.type = LOCK;} else if (line[0] == 'u') {self.type = UNLOCK;} else {self.type = END;}return is;
}
istream& operator >> (istream& is, Program& self) {auto &s = self.statements;do {s.push_back(Statement());is >> s.back();} while(s.back().type != END);return is;
}}void init() {readyQueue.clear();blockedQueue.clear();std::fill(alpha.begin(), alpha.end(), 0);lock = false;
}int main(int argc, char *argv[])
{ios::sync_with_stdio(false);int T, id = 0;cin >> T;for (int caseIdx = 0; caseIdx < T; ++caseIdx) {if (caseIdx) cout << "\n";init();cin >> n;Statement::init();cin >> quantum;string line;getline(cin, line);for (int i = 0; i < n; ++i) {readyQueue.push_back(make_shared<Program>(i + 1));cin >> *readyQueue.back();}
//        for (auto p : readyQueue) {//            cout << *p;
//        }while (!readyQueue.empty()) {//TODO:加上了&导致出错p = readyQueue.front();readyQueue.pop_front();if (p->exec()) {readyQueue.push_back(p);}
//            cout << "Test:[readyQueue]\n";
//            for (auto p : readyQueue) {//                cout << *p;
//            }
//            cout << "Test:[blockedQueue]\n";
//            for (auto p : blockedQueue) {//                cout << *p;
//            }
//            cout << flush;}
//        cout << "====================================\n";}return 0;
}

UVA - 210:Concurrency Simulator相关推荐

  1. 【日常学习】【双端队列】 Uva - 210 Concurrency Simulator题解

    ruka也的确是个小恶魔一般的东西,双端队列竟然只提了一句让我们自己去查TUT比人痛心疾首学习了众多网上神犇们的文章,终于知道了个大概.有几篇整理得非常精妙的文章,在这里给大家分享一下. http:/ ...

  2. 210 - Concurrency Simulator

    Concurrency Simulator PS:这道题目,看懂题意就费了好大功夫.跟着RuJia的程序走了一遍,调试了一遍才明白个大概,只能说基础不是很好,还需要大量的时间学习. PS:因为该题排版 ...

  3. autoware中lgsvl Simulator安装与使用:LGsvl Simulator 2021.2.1版(九)

    autoware安装与使用:LGsvl Simulator 2021.2.1版(windows10)(九) 介绍如何在windows下安装LGsvl Simulator 2021.2.1版 环境:wi ...

  4. UVA - 1103:Ancient Messages

    Ancient Messages 来源:UVA 标签: 参考资料: 相似题目: 题目 In order to understand early civilizations, archaeologist ...

  5. UVA - 136:Ugly Numbers

    Ugly Numbers 来源:UVA 标签: 参考资料:<算法竞赛入门经典>P120 相似题目: 题目 Ugly numbers are numbers whose only prime ...

  6. UVA - 514:Rails

    题目链接:https://vjudge.net/problem/UVA-514 题目分析 题目的意思是给一个栈输入一系列数据,在这个过程中可以出栈,看能否达到某个结果. 刚开始我觉得这个情况好多,因此 ...

  7. UVa 12627:Erratic Expansion(递推)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=844& ...

  8. UVa 1533:Moving Pegs(迭代加深搜索)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=842& ...

  9. micropython仿真器_microbit/cpx 的 python模拟器:Device Simulator Express

    Device Simulator Express是一个 VSCode 的编程扩展,使用它无需硬件就能对 Circuit Playground Express(CPX)或 BBC micro:bit 仿 ...

最新文章

  1. 【数学专题】整除相关 - 素数
  2. 哈希(Hash)算法是一种单向密码体制(它是一个从明文到密文的不可逆的映射只有加密过程没有解密过程)
  3. 人工智能数学基础----导数
  4. JavaScript实现完整的ComplexNumber复数类(附完整源码)
  5. 【渝粤题库】国家开放大学2021春4988电子政务概论题目
  6. 联想高校AI精英挑战赛中大收官,“人工智能眼科机器人”获全国总决赛最后入场券
  7. 设计模式C++实现——工厂模式
  8. c++ 输出格式控制
  9. zipfile.BadZipFile: File is not a zip file
  10. 微信支付phpv3给我们留下的坑
  11. HBase编程api介绍(转)
  12. 再探幻读!什么是幻读?为什么会产生幻读,MySQL中是怎么解决幻读的?
  13. 香浓熵(Shannon)与冯诺伊曼熵(Von Neumann)
  14. python 字符串 1.1 从键盘输入10个英文单词,输出其中以元音字母开头的单词。
  15. 大数据的兴起,数据科学家的崛起
  16. dfuse API 添加新分类查询,为您提供更多细化的搜索
  17. 赠书 | 供应链金融模式有哪些?区块链在供应链金融中如何应用?
  18. CPSR 和 SPSR
  19. 华维单片机编程-无线红外探测器03-环境搭建及程序详解
  20. Verilog之条件语句、循环语句、块语句与生成语句

热门文章

  1. 在Windows上安装Elasticsearch 5.0
  2. 从代码里提取的测试需求
  3. silverligh的数据访问
  4. java代码编写的文本特征提取_Test1 java语言写的特征提取源代码,有搞文字识别的可以下载一看,简单易学 Develop 274万源代码下载- www.pudn.com...
  5. linux网络唤醒,如何在Ubuntu Server 18.04中启用网络唤醒(WOL)
  6. python的颜色有哪些_Python颜色分类及格式
  7. hash的算法 java_【数据结构与算法】一致性Hash算法及Java实践
  8. python print error 空_python笔记37:10分钟掌握异常处理,再也不担心程序挂了
  9. android项目编码规范,Android 项目规范
  10. java自己实现ioc_springioc原理、springmvc项目分析、自己实现IOC