UVA - 210:Concurrency Simulator
生活随笔
收集整理的這篇文章主要介紹了
UVA - 210:Concurrency Simulator
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接: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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 成都大熊猫繁育基地能看到熊猫吗
- 下一篇: UVA - 514:Rails