网易互娱四道编程题(2020/09/05)

备战秋招面试 微信搜索公众号【TechGuide】关注更多新鲜好文和互联网大厂的笔经面经。
作者@TechGuide
点赞再看,养成习惯,您动动手指对原创作者意义非凡🤝

以下整理于牛客网

1.自动售货机( cpp实现 全A)

作者: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>
using namespace std;

struct goods {
 int price;
 stack<int> 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;
}

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 = 0
    for 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':
                if not 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 = 0
                
            else:
                lefts = 0
                
                
        else:
            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 = 0
                
            else:
                rights = 0
    print(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,0
for i in range(n):
    pre_h,pre_w=h,w
    direction, sign=arr[i]
    if sign==-1:continue
    if direction==0:h+=1
    elif direction==1:h -= 1
    elif direction==2:w-=1
    else:w += 1
    if (pre_h,pre_w) not in 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) not in dict_[key]:dict_[key][(h-1,w)]=1
    if (h+1,w) in dict_ and (h+1,w) not in dict_[key]:dict_[key][(h+1,w)]=1
    if (h,w-1) in dict_ and (h,w-1) not in dict_[key]:dict_[key][(h,w-1)]=1
    if (h,w+1) in dict_ and (h,w+1) not in dict_[key]:dict_[key][(h,w+1)]=1
seen=set()
stack=[(0,0)]
dict_[(0,0)][(0,0)]=0
while stack:
    key=stack.pop()
    if key in seen:continue
    seen.add(key)
    for w in dict_[key]:
        if w not in 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)])


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值