网易互娱21.4.18笔试代码

1. 乒乓球比赛
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <cmath>
#include <iostream>

using namespace std;

int arr[9][3][2] = {
    {{0, 1}, {2, 3}, {4, 5}},
    {{0, 1}, {2, 4}, {3, 5}},
    {{0, 1}, {2, 5}, {3, 4}},
    {{0, 2}, {1, 3}, {4, 5}},
    {{0, 2}, {1, 4}, {3, 5}},
    {{0, 2}, {1, 5}, {3, 4}},
    {{1, 2}, {0, 3}, {4, 5}},
    {{1, 2}, {0, 4}, {3, 5}},
    {{1, 2}, {0, 5}, {3, 4}},
};


int main() {
    int T;
    scanf("%d", &T);
    while(T--){
        int A[6], B[6];
        for(int i=0; i<6; i++)
            scanf("%d", &A[i]);
        for(int i=0; i<6; i++)
            scanf("%d", &B[i]);
        int ans = 0;
        for(int i=0; i<9; i++){
            for(int j=0; j<9; j++){
                int cnt = 0;
                for(int k =0; k<3; k++){
                    int temp_a, temp_b = 0;
                    temp_a = A[arr[i][k][0]] + A[arr[i][k][1]];
                    temp_b = B[arr[j][k][0]] + B[arr[j][k][1]];
                    if(temp_b > temp_a)
                        cnt ++;
                }
                if(cnt >=2)
                ans ++;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


2. 猜拳,求最大存活人数
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <cmath>
#include <iostream>

using namespace std;


int main() {
    int T;
    scanf("%d", &T);
    while(T--){
        int N, M;
        scanf("%d%d", &N, &M);
        char str[N];
        scanf("%s", str);
        // cin >> str;
        int ans = 0;
        for(int i=0; i< N; i++){
            int cur = i;
            int m = M;
            int n = N;
            string s = str;
            // cout << s.size();
            while(m--){
                if(s.size() <= 1)
                    break;
                int nex = (cur+1)%n;
                
                if(s[cur] == s[nex])
                    cur = nex;
                else if((s[cur] == 'R' && s[nex] == 'S') ||
                        (s[cur] == 'S' && s[nex] == 'C') ||
                        (s[cur] == 'C' && s[nex] == 'R')){
                            s.erase(nex, 1);
                            cur = nex < cur ? cur-1 : cur;
                            n--;
                        }
                else{
                    s.erase(cur, 1);
                    cur = nex < cur ? nex : cur;
                    n--;
                }
            }
            ans = max(ans, n);
        }
        printf("%d\n", ans);
    }
    return 0;
}



3. 游戏排行榜,中位数送礼物
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <iostream>

using namespace std;

void Insert(vector<int> &rank, int s){
    vector<int>::iterator it= rank.begin();
    // 边界问题想不动了,干脆hard coding了....
    if(rank.size()==0){
        rank.push_back(s);
        return;
    }
    if(s <= rank[0]){
        rank.insert(it, s);
        return;
    }
    for(it = rank.begin(); (it+1)!=rank.end() && (it)!=rank.end(); it++)
        if(*(it) <= s && *(it+1) >=s){
            rank.insert(it+1, s);
            return;
        }
    rank.insert(it+1, s);
}


int main() {
    int T;
    scanf("%d", &T);
    while(T--){
        int N, id, s;
        scanf("%d", &N);
        vector<int> rank;
        set<int> gift;
        map<int, int> id_s;
        for(int i=0; i<N; i++){
            scanf("%d%d", &id, &s);
            map<int, int>::iterator mpit = id_s.find(id);
            if(mpit != id_s.end() && mpit->second >= s)
                    continue;
            if(mpit != id_s.end())
                for(vector<int>::iterator it = rank.begin(); it!=rank.end(); it++)
                    if(*(it) == mpit->second){
                        rank.erase(it);
                        break;
                    }
            Insert(rank, s);
            int n_rank = rank.size();
            if(n_rank % 2 == 1){
                if( s == rank[n_rank /2])
                    gift.insert(id);
            }
            else{
                if((rank[n_rank/2] + rank[n_rank/2 -1]) % 2 == 0 && 
                    (rank[n_rank/2] + rank[n_rank/2 -1]) / 2 == s)
                    gift.insert(id);
            }
            id_s[id] = s;
        }
        printf("%d\n", gift.size());
    }
    return 0;
}


#笔经##Java工程师#
全部评论
大佬好,我想问下rank排序为啥不用sort函数,要自己写个Insert呢,自己写的Insert最差能到O(N^2)吧,相对于sort的O(NlogN)反而差了
点赞 回复 分享
发布于 2021-04-22 21:25
😣天啊 看了你代码才反应过来我是不是读错题了···第二题是要以任一小朋友开始猜拳,计算经历M场猜拳最大能留在场上的小朋友数吗
点赞 回复 分享
发布于 2021-04-19 14:17
有一说一,最后一题n^2能过是数据水了
点赞 回复 分享
发布于 2021-04-19 09:22
网易游戏跟网易互娱原来笔试是分开的吗
点赞 回复 分享
发布于 2021-04-18 21:25
第二题是暴力吗😂,我操作了半天把自己操作没了
点赞 回复 分享
发布于 2021-04-18 19:22
为什么第一题我也是穷举81,但是时间卡死在1001ms了。
点赞 回复 分享
发布于 2021-04-18 19:15

相关推荐

04-08 23:14
已编辑
南阳理工学院 算法工程师
本人情况:26届双非本科,两段实习经历,目前拿到的都是实习的offer,一个校招的都没有,他们都说先实习,然后等拿到毕业证了直接转正,我又害怕干三个月给我叉出去面试题也发一下吧###&nbsp;杭州问尔信息技术后端登录你是怎么做的?JWT令牌你了解过吗?他虽然是一段字符串,他表达了什么东西?怎么解析出来信息和过期时间?JWT令牌怎么续期?如果我拉黑一个账号,要怎么做?两种方案(有无redis)mongodb和mysql的区别?mongodb和mysql分别实用于什么项目?选型你会怎么选?数据库的事务,那些地方需要使用,那些地方不需要使用?他会影响什么性能?mysql和pgsql有什么差异你知道吗?消息队列&nbsp;redis也有,为什么要用mq?前后端会部署吗?docker会用吗?内部通信前端&nbsp;async和&nbsp;await你知道吗?异步编程的原理是什么?vue3&nbsp;为什么你改变一个字符串&nbsp;前端会跟着改动AI工具会用什么?你会怎么用?###&nbsp;仲财通常用的锁有哪些synchronize和ReentrantLock的区别分布式锁了解吗?分布式事务mysql表字段sql优化什么时候用索引索引什么时候会失效mysql事务ioc一些项目应用问题###&nbsp;观妙科技项目问题...zset的架构是什么样子的线程池突然队列被打满了怎么办?如果上游和下游都无法控制,该怎么维护select&nbsp;*&nbsp;from&nbsp;user&nbsp;where&nbsp;age&gt;20&nbsp;order&nbsp;by&nbsp;update_time&nbsp;索引设计检索过程是什么样的冒泡排序和快排,有什么区别怎么判断链表有没有环###&nbsp;观妙科技-二面项目部分...线程池的核心参数有哪些你是怎么用线程池的JMMG1模型跳表介绍一下平衡二叉树TCP为什么要三次握手?说一下hashmap红黑树的特征你有什么学习的方法
牛马人的牛马人生:对学院本而言很强了
点赞 评论 收藏
分享
评论
6
22
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务