京东笔试 20230826

计算机基础单选+三种语言任选一种的语言特性题单选+3道编程。

单选都不太难,有个考数据库的,问给定条件下页表目录多大的题 没答上来。

编程:第一题,输入一个数组 $a_i$ 满足长度小于1e5,值小于1e6。输出数组 $b_i$ 使得对于任意的i,$(a_i + b_i) % i = 0$,1<=b_i<=1e9且 $b_i$ 各不相同。

感觉就是瞎搞。如果有重复值了就加上几个i直到没碰撞为止。AC

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 20;
const int INF = 1e9;

long long a[N];
int cnt = 0;
set<int> st;
set<int> ss;

int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        int val;
        cin >> val;
        a[i] = i - val + val / i * i;
    }

    for (int i = n; i >= 1; --i)
    {
        while(st.count(a[i]))
            a[i] += 1LL * i * (++cnt);
        if(a[i] > INF)
            a[i] -= INF / i * i;
        while(st.count(a[i]))
            a[i] += 1LL * i * (++cnt);
        st.insert(a[i]);
    }

    for (int i = 1; i <= n; ++i)
    {
        cout << a[i] << ' ';
        if(a[i] >= 1e9 || ss.count(a[i]))
            while(true);
        ss.insert(a[i]);
    }
    cout << endl;
    return 0;
}

第二题,兽棋。有n颗棋子,碰面了m次,问最后哪些活着。棋子身份为人或兽,各有战斗值$a_i$。每次战斗前可以选择说出自己的身份或不说。兽知道了对方是人,则一定战斗。人知道对方是兽且自己战力更高时,才会发起战斗。身份相同则无事发生。每次战斗战力低的死亡,相等则同时死亡。如果有一方已经死了则不会发起战斗。

模拟。没少写特判条件,但是就过了95%。

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 20;

int n, m;
int type[N], val[N], die[N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        string s;
        cin >> s >> val[i];
        type[i] = s[0] == 'h';
    }
    for (int i = 1; i <= m; ++i) {
        int x, y;
        string a, b;
        cin >> x >> y >> a >> b;
        if (die[x] || die[y] || (a[0] == 'N' && b[0] == 'N') || type[x] == type[y])
            continue;
        if (a[0] == 'Y' && b[0] == 'Y') {
            if (val[x] > val[y])
                die[y] = 1;
            else if (val[x] < val[y])
                die[x] = 1;
            else
                die[x] = die[y] = 1;
        } else if (a[0] == 'Y') {
            if (type[y]) {
                if (val[y] > val[x])
                    die[x] = 1;
            } else {
                if (val[x] > val[y])
                    die[y] = 1;
                else if (val[x] < val[y])
                    die[x] = 1;
                else
                    die[x] = die[y] = 1;
            }
        } else {
            if (type[x]) {
                if (val[x] > val[y])
                    die[y] = 1;
            } else {
                if (val[x] > val[y])
                    die[y] = 1;
                else if (val[x] < val[y])
                    die[x] = 1;
                else
                    die[x] = die[y] = 1;
            }
        }
    }
    for (int i = 1; i <= n; ++i)
        cout << (die[i] ? 'N' : 'Y');
    cout << endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

第三题,n道题,m秒。每题可以选择花 at_i 秒得 ap_i 分或 bt_i 秒得 bp_i分或不做,问最后最多能得多少分。

01背包,存做了前 i 题,花 j 秒能获得的最高分,同时存当前状态下每题是得了ap还是bp还是放弃了。AC

#include <bits/stdc++.h>
using namespace std;
const int N = 2020;

int n, m;
int pt[N], pp[N], at[N], ap[N];
vector< vector<int> > res(2, vector<int>(N));
int fi[N];
bitset<N> bt1[2][N], bt2[2][N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        cin >> at[i] >> ap[i] >> pt[i] >> pp[i];
    }
    for (int i = 1; i <= n; ++i) {
        string ss = to_string(i);
        for (int j = 0; j <= m - at[i]; ++j) {
            if (res[1][j + at[i]] < res[0][j] + ap[i]) {
                res[1][j + at[i]] = res[0][j] + ap[i];
                bt1[1][j + at[i]] = bt1[0][j];
                bt2[1][j + at[i]] = bt2[0][j];
                bt1[1][j + at[i]][i].flip();
            }
        }
        for (int j = 0; j <= m - pt[i]; ++j) {
            if (res[1][j + pt[i]] < res[0][j] + pp[i]) {
                res[1][j + pt[i]] = res[0][j] + pp[i];
                bt1[1][j + pt[i]] = bt1[0][j];
                bt2[1][j + pt[i]] = bt2[0][j];
                bt2[1][j + pt[i]][i].flip();
            }
        }
        for (int i = 1; i <= m; ++i)
            bt1[0][i] = bt1[1][i], bt2[0][i] = bt2[1][i], res[0][i] = res[1][i];
    }
    for(int i=1;i<=n;++i)
        cout << (bt1[1][m][i] ? 'A' : (bt2[1][m][i] ? 'B' : 'F'));

    cout << endl;
    return 0;
}

#京东笔试#
全部评论
牛蛙
点赞 回复 分享
发布于 2023-08-26 22:37 北京

相关推荐

溱元:前端每年固定死几次,看两集广告就复活了
点赞 评论 收藏
分享
首先讲三个故事,关于牛客的事件一:2024年,牛客上有一对高学历情侣,求职方向与我当时一致,都是嵌入式方向。他们恰好是我的朋友,专业能力和学历背景都很扎实,也因此拿到了不少优质offer。和很多求职者一样,他们把offer情况整理后发在平台上,本意是记录与交流,但很快引发了争议。有声音指责他们“集邮”“不释放名额”,认为这种展示本身就是一种炫耀。最终讨论失控,当事人删除内容,事件也很快被遗忘。事件二:小红书评论区,一条评价获得了不少共鸣:“感觉牛客就是当年那群做题区毕业了开始找工作还收不住那股味,颇有一种从年级第一掉到年纪第二后抱怨考不上大学的味道”,这条评论被水印里这个同学转发到牛客后,评论...
小型域名服务器:当看到别人比自己强的时候,即便这是对方应得的,很多人会也下意识的歪曲解构对方的意图,来消解自己在这本就不存在的比较中输掉的自信,从而平白制造出很多无谓的争论。比如你会在空余时间来写优质好文,而我回家只会暗区突围,那么我就可以作为键盘侠在这里评论你是不是XXXXXXXX。即便我自己都知道这是假的,但只要这没那么容易证伪,那么当你开始回应的时候,脏水就已经泼出去了,后面可能会有更多的人带着情绪来给我点赞,而毫不关注你写的文章内容本身是啥了。
SAGIMA牛马咖啡
点赞 评论 收藏
分享
评论
3
9
分享

创作者周榜

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