网易互娱数据研发工程师笔试题解

第1题:角色吻合度 60%

#include <bits/stdc++.h>
#include <iomanip>
#define LL long long
using namespace std;

const int maxn = 1e7 + 5;
const double eps = 1e-4;

struct ST {
    LL login, logout;
    LL online;
    int roleid;
};

bool cmp(ST a, ST b) {
    return a.roleid < b.roleid;
}

ST newRole[maxn];
ST role[maxn];

int main()
{
    string type;
    string data;

    for (int i = 0; i < maxn; i++) {
        role[i].logout = -1;
    }
    while (cin >> data) {
        int len = data.size();
        LL time = 0, roleid = 0;
        string type = "";
        bool flag1 = true, flag2 = true, flag3 = false;
        for (int i = 0; i < len; i++) {
            if (data[i] == '[') continue;
            if (data[i] == ']') {
                if (!flag1) flag2 = false;
                flag1 = false;
                continue;
            }
            if (flag1) time = time * 10 + data[i] - '0';
            if (data[i] == '\"' && flag3) flag3 = false;
            if ((i > 2 && data[i - 1] == '\"' && data[i - 2] == ':') || flag3) {
                flag3 = true;
                roleid = roleid * 10 + data[i] - '0';
            }
            if (!flag1 && flag2) type += data[i];
        }
        //cout << time << ' ' << type << ' ' <<roleid << endl;
        if (type == "login") role[roleid].login = time;
        else if (type == "logout") role[roleid].logout = time;
    }

    int cnt = 0;
    for (int i = 0; i < maxn; i++) {
        if (role[i].logout != -1) {
            newRole[cnt].login = role[i].login;
            newRole[cnt].logout = role[i].logout;
            newRole[cnt].online = role[i].logout - role[i].login;
            newRole[cnt++].roleid = i;
        }
    }
    sort(newRole, newRole + cnt, cmp);
    int ans1[maxn], ans2[maxn], dex = 0;
    set<int> ans;
    LL tmp = -1;
    for (int i = 0; i < cnt; i++) {
        for (int j = i + 1; j < cnt; j++) {
            LL a = max(newRole[i].login, newRole[j].login);
            LL b = min(newRole[i].logout, newRole[j].logout);
            LL val = (b - a) * 2.0 / (newRole[i].online + newRole[j].online);
            if (val > tmp) {
                tmp = val;
                dex = 0;
                ans.clear();
            }
            if (abs(tmp - val) < eps) {
                ans.insert(newRole[i].roleid);
                ans.insert(newRole[j].roleid);
                ans1[dex] = newRole[i].roleid;
                ans2[dex++] = newRole[j].roleid;
            }
        }
    }
    for (int i : ans) cout << i << endl;
    return 0;
}

第2题:左手规则走迷宫 0%

#include <bits/stdc++.h>
#include <iomanip>
#define LL long long
using namespace std;

const int maxn = 1e6 + 5;
int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

int main()
{
    int t;
    cin >> t;
    while (t--) {
        int w, h;
        cin >> w >> h;
        char mat[45][45];
        int sx, sy, direct;
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                cin >> mat[i][j];
                if (mat[i][j] == 'S') {
                    sx = i;
                    sy = j;
                    if (sx == 0) direct = 2;
                    else if (sy == 0) direct = 1;
                    else if (sx == w - 1) direct = 0;
                    else direct = 3;
                }
            }
        }
        int cnt = 0;
        //cout << sx << ' ' << sy << endl;
        //cout << direct << endl;
        while (1) {
            int nx = sx + dir[direct][0], ny = sy + dir[direct][1];
            //cout << nx << ' ' << ny << endl;
            if (nx < 0 || nx >= w || ny < 0 || ny >= h || mat[nx][ny] == '#') direct = (direct + 1) % 4;
            else if (mat[nx][ny] == 'E') {
                cout << cnt << endl;
                break;
            }
            else {
                sx = nx;
                sy = ny;
                cnt++;
                direct = (direct - 1) % 4;
                cout << nx << ' ' << ny << endl;
            }
        }
    }
    return 0;
}

/*
2
5 5
#####
#...#
#...#
#.#.#
#E#S#
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########

7
17
*/

第3题:根据SQL语句输出查询结果 100%

#include <bits/stdc++.h>
#include <iomanip>
#define LL long long
using namespace std;

const int maxn = 1e7 + 5;
struct ST {
    LL id, age, height, weight;
    LL id1, id2;
    double val;
};

const double eps = 1e-9;
bool cmp(ST a, ST b) {
    if (a.id1 == b.id1) {
        if (abs(a.val - b.val) < eps) return a.id2 < b.id2;
        return a.val < b.val;
    }
    return a.id1 < b.id1;
}

LL compute(LL a, LL b)
{
    LL ans = (a - b)*(a - b);
    return ans;
}

ST table[maxn];
ST newTable[maxn];

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int id, age, height, weight;
        cin >> id >> age >> height >> weight;
        table[i].id = id;
        table[i].age = age;
        table[i].height = height;
        table[i].weight = weight;
    }
    int cnt = 0;
    set<int> s;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (table[i].id != table[j].id) {
                double val = sqrt(compute(table[i].age, table[j].age) + compute(table[i].height, table[j].height) + compute(table[i].weight, table[j].weight));
                if (val < 20) {
                    s.insert(table[i].id);
                    newTable[cnt].id1 = table[i].id;
                    newTable[cnt].id2 = table[j].id;
                    newTable[cnt++].val = val;
                }
            }
        }
    }
    sort(newTable, newTable + cnt, cmp);
    int vis[maxn];   // group by id1
    memset(vis, 0, sizeof(vis));
    for (int i = 0; i < cnt; i++) {
        if (!vis[newTable[i].id1] && s.find(newTable[i].id1) != s.end()) {
            vis[newTable[i].id1] = 1;
            cout << newTable[i].id1 << ' ' << newTable[i].id2 << ' ';
            printf("%.2f\n", newTable[i].val);
        }
    }
    return 0;
}
#网易##网易互娱##题解#
全部评论
大佬面了吗?
点赞 回复 分享
发布于 2018-09-18 17:41
楼主,请问昨天SQL那道题,select后面没有聚合函数呀,为什么要加group by id1,被这个搞懵了。看你的解答好像是id1相同的只保留一项,是这样么?
点赞 回复 分享
发布于 2018-09-17 22:02
我就第一题忘了考虑两位数。。悲剧。。一共就过了40%,没戏了
点赞 回复 分享
发布于 2018-09-16 21:54

相关推荐

不愿透露姓名的神秘牛友
09-23 14:18
京✌们还有比我低的吗
迷茫的大四🐶:这工资还能干下去怕不是体制内哦,要不然我想不出来留下来的理由
点赞 评论 收藏
分享
将不会再购买联想产品
Data_Seven:让他发一个拯救者过来 拯救一下你
投递联想等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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