9.5网易笔试 一四题ac代码 求二三题


蹲一个二三题的思路或者代码

第一题ac 自动售卖机

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <stack>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <unordered_map>
#include "limits.h"
#include "math.h"
using namespace std;

int main(int argc, const char * argv[]) {
    int n, m;
    cin >> n >> m;
    //每个货物槽的原始价格
    int prices[n];
    for(int i=0; i<n; i++){
        cin >> prices[i];
    }
    //n个货物槽:记录可能被错放的货物的价格
    vector<stack<int> > boxes;
    for(int i=0; i<n; i++){
        stack<int> st;
        boxes.push_back(st);
    }
    //m个人进行操作
    int k, num;
    string hand, action;
    while( m-- ){
        cin >> k; //k次操作
        int left = -1, right = -1; //左右手货物的价格
        int p, totalprice = 0; //总价格
        while( k-- ){
            cin >> hand >> action;
            if( action=="take" ){
                cin >> num;
                if( boxes[num-1].empty() ){
                    p = prices[num-1];
                }else{
                    p = boxes[num-1].top();
                    boxes[num-1].pop();
                }
                totalprice += p;
                if( hand=="left" ){
                    left = p;
                }else{
                    right = p;
                }
            }else if( action=="return" ){
                cin >> num;
                if( hand=="left" ){
                    p = left;
                    left = -1;
                }else{
                    p = right;
                    right = -1;
                }
                boxes[num-1].push(p);
                totalprice -= p;
            }else{ //keep
                if( hand=="left" ){
                    left = -1;
                }else{
                    right = -1;
                }
            }
        }
        cout << totalprice << endl;
    }

    return 0;
}
/*
 5 3
 1 2 3 4 5
 5
 left take 1
 right take 2
 left return 3
 right keep
 right take 4
 6
 left take 5
 right take 1
 left return 2
 right return 3
 left take 5
 left return 4
 10
 left take 1
 left keep
 left take 2
 left keep
 left take 3
 left keep
 left take 4
 left keep
 left take 5
 left keep
 */

第二题 游戏机刷新帧

理解题目之后觉得很难的样子 直接跳了

第三题 迷宫

乍一看以为简单,先根据输入构建map,后期bfs。结果超内存才发现输入最多有1000000,这没法预先定义map大小啊...然后就卡住了

第四题ac 非凡排序

要求a[i]*a[i+1]<=a[i+1]*a[i+2],根据正负去分别判断,保证乘积为负的放左边,乘积为正的放左边
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <unordered_map>
#include "limits.h"
#include "math.h"
using namespace std;

bool cmp(int a, int b){
    return a > b;
}
int main(int argc, const char * argv[]) {
    //输入有正有负
    int n, num;
    cin >> n;
    vector<int> posi;
    vector<int> nega;
    for(int i=0; i<n; i++){
        cin >> num;
        if( num<0 ){
            nega.push_back(num);
        }else{
            posi.push_back(num);
        }
    }
    //正数更多:整个正数升序排序
    //(负 正 负 正):正数逆序,负数正序 + (正...正)正数升序排序
    if( posi.size() >= nega.size() ){
        sort(posi.begin(), posi.end()); //升序
        sort(nega.begin(), nega.end()); //升序
        int i=0, j=nega.size()-1;
        for(; i<nega.size(); i++,j--){
            cout << nega[i] << " " << posi[j] << " ";
        }
        for(; i<posi.size(); i++){
            cout << posi[i] << " ";
        }
        cout << endl;
    }else{
        //负数整体逆序
        //(正 负 正 负):正数逆序,负数正序 + (负...负)负数逆序
        sort(posi.begin(), posi.end(), cmp); //逆序
        sort(nega.begin(), nega.end(), cmp); //逆序
        int i=0, j=posi.size()-1;
        for(; i<posi.size(); i++, j--){
            cout << posi[i] << " " << nega[j] << " ";
        }
        for(; i<nega.size(); i++){
            cout << nega[i] << " ";
        }
        cout << endl;
    }
    
    
    return 0;
}
/*
 6
 -2 4 -1 3 5 6
 10
 9 8 7 -1 -2 -3 -4 -5 -6 -7
 */


#笔试题目##网易#
全部评论
https://www.nowcoder.com/discuss/500137?source_id=profile_create&channel=1009 无T2有T3
1 回复
分享
发布于 2020-09-05 18:03

相关推荐

点赞 1 评论
分享
牛客网
牛客企业服务