做了两道题,第一题IDE调试的,第三题用的哈希表+BFS
第一题代码:
#include <iostream> #include <stack> #include <vector> #include <sstream> using namespace std; int main() { int N, M; cin >> N >> M; vector<int> prices(N + 1, 0); for (int i = 1; i <= N; ++i) cin >> prices[i]; vector<stack<int>> slots(N + 1); for (int i = 0; i < M; ++i) { int count; cin >> count; int hand[2] = { 0,0 }; //left:0 right:1 int sum = 0; string tt; getline(cin, tt); //删除换行符 for (int j = 0; j < count; ++j) { string s, str; int handidx = -1; getline(cin, s); stringstream ss(s); ss >> str; if (str == "left") handidx = 0; else handidx = 1; ss >> str; if (str == "take") { int goods; ss >> goods; if (!slots[goods].empty()) { hand[handidx] = slots[goods].top(); slots[goods].pop(); } else hand[handidx] = prices[goods]; } else if (str == "return") { int goods; ss >> goods; slots[goods].push(hand[handidx]); hand[handidx] = 0; } else { sum += hand[handidx]; hand[handidx] = 0; } } cout << sum + hand[0] + hand[1] << endl; } return 0; }
全部评论
(0) 回帖