作者:show_me_offer
链接:https://www.nowcoder.com/discuss/500024?type=3&channel=666&source_id=discuss_center_discuss_hot
来源:牛客网
#include<iostream>#include<vector>#include<stack>#include<string>usingnamespace std;struct goods {
int price;
stack<int> others;};intmain(){
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();}}elseif(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();}}elseif(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;return0;}
1.自动售货机( python实现 全A)
作者:荔枝小杨
链接:https://www.nowcoder.com/discuss/500024?type=3&channel=666&source_id=discuss_center_discuss_hot
来源:牛客网
import sys
N,M =map(int,input().split())
prices =list(map(int,input().split()))
map_ ={
} #保存每个槽柜的价格序列
for i in range(N):
map_[i]=[prices[i]]# print(map_)for i in range(M): #读取每个人的操作记录
money =0 #每个人应该支付的金额
line_ =int(sys.stdin.readline().strip())# print(k)
lefts =0 #保存手里物品的价格
rights =0for j in range(line_): #每个人共k个操作
# print(map_)
record = sys.stdin.readline().strip()
rec = record.split()
hand = rec[0]
op = rec[1]if op =='take'or op =='return':
id_ =int(rec[2]) #槽柜编号
# print(hand)if hand =='left':if op =='take':ifnot map_[id_-1]:
lefts = prices[id_-1]else:
lefts = map_[id_-1][-1]
map_[id_-1].pop()
money+=lefts
elif op =='return':
p = lefts
money -= lefts
map_[id_-1].append(p)
lefts =0else:
lefts =0else:if op =='take':if map_[id_-1]==[]:
rights = prices[id_-1]else:
rights = map_[id_-1][-1]
map_[id_-1].pop()
money+=rights
elif op =='return':
p = rights
money -= rights
map_[id_-1].append(p)
rights =0else:
rights =0print(money)
2.绘制屏幕
作者:chaiser
链接:https://www.nowcoder.com/discuss/500144?type=post&order=time&pos=&page=1&channel=666&source_id=search_post
来源:牛客网
cin >> h >> l >> speedl >> speedh;while(true){
screen = draw(background, chara, h-1, l-1);//在背景图指定位置画个角色
int diff = different(screen, lastscreen);//和上一张图区别多少字符
lastscreen = screen;
h += speedh;
l += speedl;sum+= diff;if(h > H && speedh >=0)break;//这段好像可以写在开始 无所谓了
if(l > W && speedl >=0)break;if(h + pH <2&& speedh <=0)break;if(l + pW <2&& speedl <=0)break;}sum+= different(background, screen);
3.迷宫
作者:君子有酒
链接:https://www.nowcoder.com/discuss/500082?type=post&order=time&pos=&page=1&channel=666&source_id=search_post
来源:牛客网
dict_={
}
n=8
arr=[[0,1],[0,1],[3,1],[3,1],[1,1],[1,1],[2,1],[0,1]]
h,w=0,0for i inrange(n):
pre_h,pre_w=h,w
direction, sign=arr[i]if sign==-1:continueif direction==0:h+=1elif direction==1:h -=1elif direction==2:w-=1else:w +=1if(pre_h,pre_w)notin dict_:
dict_[(pre_h,pre_w)]={
}
dict_[(pre_h,pre_w)][(h,w)]=1
dict_[(h,w)]={
}
final_h,final_w=h,w
for key in dict_:
h, w = key
if(h-1,w)in dict_ and(h-1,w)notin dict_[key]:dict_[key][(h-1,w)]=1if(h+1,w)in dict_ and(h+1,w)notin dict_[key]:dict_[key][(h+1,w)]=1if(h,w-1)in dict_ and(h,w-1)notin dict_[key]:dict_[key][(h,w-1)]=1if(h,w+1)in dict_ and(h,w+1)notin dict_[key]:dict_[key][(h,w+1)]=1
seen=set()
stack=[(0,0)]
dict_[(0,0)][(0,0)]=0while stack:
key=stack.pop()if key in seen:continue
seen.add(key)for w in dict_[key]:if w notin dict_[(0,0)]:
dict_[(0,0)][w]=dict_[key][w]+dict_[(0,0)][key]else:
dict_[(0,0)][w]=min(dict_[key][w]+dict_[(0,0)][key], dict_[(0,0)][w])
stack.append(w)print(dict_[(0,0)][(final_h,final_w)])