题解
原题传送门
这是一篇 Python 题解,文末也会附上 C++ 的代码。
对于这道题,我们可以直接模拟。
设cl为现在物品的颜色,lk为现在物品的上锁情况。若lk为
记得要初始化。
代码如下:
Python
n=int(input())
lk=0
cl="blue"
for i in range(n):
s=input()
if s=="lock": #上锁
lk=1
elif s=="unlock": #开锁
lk=0
elif lk==0: #更新颜色
cl=s
print(cl)
C++
#include<iostream>
#include<string>
using namespace std;
int n;
bool lk;
string cl="blue";
int main(){
cin>>n;
for(int i=1;i<=n;i++){
string s;
cin>>s;
if(s=="lock")lk=1; //上锁
else if(s=="unlock")lk=0; //开锁
else if(lk==0)cl=s; //更新颜色
}
cout<<cl;
return 0;
}