1.模拟贩卖机 2.打印屏幕 3.迷宫(没考虑周全)
没想到自定义结构体
1.#include
#include
#include
#include
using namespace std;
struct goods {
int price;
stack others;
};
int main() {
int N, M;
cin >> N >> M;
goods* mp = new goods[N + 1];
for(int i=1;i<=N;++i){
cin >> mp[i].price;
}
int money = 0;
for (int i = 0; i < M; ++i) {
money = 0;
int left = 0; // 左手商品价钱
int right = 0; // 右手商品价钱
string str1, str2; // 接收每次的前两个操作符
int No; // 第三个操作参数
int operateTimes = 0;
cin >> operateTimes;
for (int j = 0; j < operateTimes; ++j) {
cin >> str1 >> str2;
if (str1 == “left”) {
if (str2 == “take”) {
cin >> No;
if (mp[No].others.empty()) {
left = mp[No].price;
}
else {
left = mp[No].others.top();
mp[No].others.pop();
}
}
else if (str2 == “return”) {
cin >> No;
mp[No].others.push(left);
left = 0;
}
else { // str2 = keep
money += left;
left = 0;
}
}
else {
if (str2 == “take”) {
cin >> No;
if (mp[No].others.empty()) {
right = mp[No].price;
}
else {
right = mp[No].others.top();
mp[No].others.pop();
}
}
else if (str2 == “return”) {
cin >> No;
mp[No].others.push(right);
right = 0;
}
else { // str2 = keep
money += right;
right = 0;
}
}
}
money = money + left + right;
cout << money << endl;
}
delete[] mp;
return 0;
}
2,3等牛客或者微信cpp答案