饿了么笔试 饿了么笔试题 0815

笔试时间:2025年8月15日

往年笔试合集:

2023春招秋招笔试合集

2024春招秋招笔试合集

第一题

给你一个整数 n,让你构造一个长度为 n 的数组 a,使得 a₁⊕a₂…aₙ == a₁|a₂…aₙ。

输入格式,第一行一个整数 T 代表样例个数,一次 T 行,一行一个整数 n

样例输入

2

4

2

样例输出

4 6 3 3

2 4

参考题解

C++:

#include <iostream>

int main() {
    int T;
    std::cin >> T;
    while (T--) {
        int n;
        std::cin >> n;
        if (n == 1) {
            std::cout << "114514\n";
        } elseif (n == 2) {
            std::cout << "1 2" << "\n";
        } else {
            if (n % 2 == 0) {
                std::cout << "1 2" << "\n";
                for (int i = 2; i < n; ++i) {
                    std::cout << 3 << " \n"[i == n - 1];
                }
            } else {
                for (int i = 0; i < n; ++i) {
                    std::cout << 114514 << "\n";
                }
            }
        }
    }
}

Java:

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        FastScanner fs = new FastScanner(System.in);
        StringBuilder out = new StringBuilder();
        int T = fs.nextInt();
        while (T-- > 0) {
            int n = fs.nextInt();
            if (n == 1) {
                out.append("114514\n");
            } else if (n == 2) {
                out.append("1 2\n");
            } else {
                if (n % 2 == 0) {
                    out.append("1 2\n");
                    for (int i = 0; i < n - 2; i++) {
                        if (i > 0) out.append(' ');
                        out.append('3');
                    }
                    out.append('\n');
                } else {
                    for (int i = 0; i < n; i++) {
                        out.append("114514\n");
                    }
                }
            }
        }
        System.out.print(out.toString());
    }

    // 简单快读
    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        FastScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); }
        String next() throws IOException {
            while (st == null || !st.hasMoreTokens()) {
                String line = br.readLine();
                if (line == null) return null;
                st = new StringTokenizer(line);
            }
            return st.nextToken();
        }
        int nextInt() throws IOException { return Integer.parseInt(next()); }
    }
}

Python:

import sys

def main():
    data = sys.stdin.read().strip().split()
    it = iter(data)
    T = int(next(it))
    out_lines = []
    for _ in range(T):
        n = int(next(it))
        if n == 1:
            out_lines.append("114514")
        elif n == 2:
            out_lines.append("1 2")
        else:
            if n % 2 == 0:
                out_lines.append("1 2")
                out_lines.append(" ".join(["3"] * (n - 2)))
            else:
                out_lines.extend(["114514"] * n)
    sys.stdout.write("\n".join(out_lines))

if __name__ == "__main__":
    main()

第二题

给你一个长度为 n 的数组 ,代表了a根木棍。要求输出n-2个答案,第 i 个答案是由组成的正i多边形的方案数(对 998244353 取模)

样例输入

8

1 2 1 1 3 2 2 2

样例输出

5 1 0 0 0

参考题解

直接使用一个哈希表计数,统计每个长度的个数,随后枚举长度和边数,统计答案即可

C++:

#include <bits/stdc++.h>
usingnamespacestd;

constint MOD = 998244353;
using i64 = longlong;

i64 qpow(i64 a, i64 b) {
    i64 res = 1;
    while (b) {
        if (b & 1) res = res * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}

void solve() {
    int n;
    cin >> n;
    vector<i64> a(n), fact(n+1), invfact(n+1);
    map<i64,int> cnt;

    for (int i = 0; i < n; i++) {
        cin >> a[i];
        cnt[a[i]]++;
    }

    // 组合数预处理
    fact[0] = 1;
    for (int i = 1; i <= n; i++) fact[i] = fact[i-1] * i % MOD;
    invfact[n] = qpow(fact[n], MOD-2);
    for (int i = n-1; i >= 0; i--) invfact[i] = invfact[i+1] * (i+1) % MOD;

    auto C = [&](int N, int K) -> i64 {
        if (K < 0 || K > N) return0;
        return fact[N] * invfact[K] % MOD * invfact[N-K] % MOD;
    };

    vector<int> ans;
    for (int len = 3; len <= n; ++len) {
        i64 tot = 0;
        for (auto [_, v] : cnt) {
            if (v >= len) {
                tot = (tot + C(v, len)) % MOD;
            }
        }
        ans.push_back(tot);
    }

    for (int i = 0; i < (int)ans.size(); i++) {
        cout << ans[i] << (i+1 == ans.size() ? '\n' : ' ');
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return0;
}

Java:

import java.io.*;
import java.util.*;

public class Main {
    static final long MOD = 998244353L;

    static long modPow(long a, long b) {
        long res = 1 % MOD;
        a %= MOD;
        while (b > 0) {
            if ((b & 1) == 1) res = (res * a) % MOD;
            a = (a * a) % MOD;
            b >>= 1;
        }
        return res;
    }

    public static void main(String[] args) throws Exception {
        FastScanner fs = new FastScanner(System.in);
        int n = fs.nextInt();
        long[] a = new long[n];
        for (int i = 0; i < n; i++) a[i] = fs.nextLong();

        // 频次统计
        HashMap<Long, Integer> cnt = new HashMap<>();
        for (long x : a) cnt.put(x, cnt.getOrDefault(x, 0) + 1);

        // 阶乘与逆元
        long[] fact = new long[n + 1];
        long[] invfact = new long[n + 1];
        fact[0] = 1;
        for (int i = 1; i <= n; i++) fact[i] = (fact[i - 1] * i) % MOD;
        invfact[n] = modPow(fact[n], MOD - 2);
        for 

剩余60%内容,订阅专栏后可继续查看/也可单篇购买

2025 春招笔试合集 文章被收录于专栏

2025打怪升级记录,大厂笔试合集 C++, Java, Python等多种语言做法集合指南

全部评论

相关推荐

第一题:n为奇数输出n个1,n为偶数输出n-1个0即可:#include&nbsp;&lt;iostream&gt;using&nbsp;namespace&nbsp;std;int&nbsp;main()&nbsp;{int&nbsp;t;cin&nbsp;&gt;&gt;&nbsp;t;while&nbsp;(t&nbsp;--)&nbsp;{int&nbsp;n;cin&nbsp;&gt;&gt;&nbsp;n;if&nbsp;(n&nbsp;&amp;&nbsp;1)&nbsp;{for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;n;&nbsp;i&nbsp;++)&nbsp;{cout&nbsp;&lt;&lt;&nbsp;1&nbsp;&lt;&lt;&nbsp;&quot;&nbsp;&quot;;}cout&nbsp;&lt;&lt;&nbsp;'\n';}&nbsp;else&nbsp;{for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;n&nbsp;-&nbsp;1;&nbsp;i&nbsp;++)&nbsp;{cout&nbsp;&lt;&lt;&nbsp;1&nbsp;&lt;&lt;&nbsp;&quot;&nbsp;&quot;;}cout&nbsp;&lt;&lt;&nbsp;0&nbsp;&lt;&lt;&nbsp;'\n';}}}第二题:如果你有n个长度相同的木棍,那么他们组成正m边形的组合是C(n,m)个,C是组合数,计下数就可以了。#include&nbsp;&lt;bits/stdc++.h&gt;using&nbsp;namespace&nbsp;std;#define&nbsp;int&nbsp;long&nbsp;longconst&nbsp;int&nbsp;p&nbsp;=&nbsp;998244353;const&nbsp;int&nbsp;N&nbsp;=&nbsp;5e3&nbsp;+&nbsp;10;int&nbsp;h[N],&nbsp;rh[N];int&nbsp;C(int&nbsp;n,&nbsp;int&nbsp;m)&nbsp;{if&nbsp;(n&nbsp;&lt;&nbsp;m)&nbsp;return&nbsp;0;return&nbsp;h[n]&nbsp;*&nbsp;rh[m]&nbsp;%&nbsp;p&nbsp;*&nbsp;rh[n&nbsp;-&nbsp;m]&nbsp;%&nbsp;p;}int&nbsp;qs(int&nbsp;a,&nbsp;int&nbsp;b)&nbsp;{int&nbsp;res&nbsp;=&nbsp;1;while&nbsp;(b)&nbsp;{if&nbsp;(b&nbsp;&amp;&nbsp;1)&nbsp;res&nbsp;=&nbsp;res&nbsp;*&nbsp;a&nbsp;%&nbsp;p;a&nbsp;=&nbsp;a&nbsp;*&nbsp;a&nbsp;%&nbsp;p;b&nbsp;&gt;&gt;=&nbsp;1;}return&nbsp;res;}signed&nbsp;main()&nbsp;{h[0]&nbsp;=&nbsp;rh[0]&nbsp;=&nbsp;1;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;1;&nbsp;i&nbsp;&lt;&nbsp;N&nbsp;;&nbsp;i&nbsp;++)&nbsp;{h[i]&nbsp;=&nbsp;h[i&nbsp;-&nbsp;1]&nbsp;*&nbsp;i&nbsp;%&nbsp;p;rh[i]&nbsp;=&nbsp;qs(h[i],&nbsp;p&nbsp;-&nbsp;2);}int&nbsp;n;cin&nbsp;&gt;&gt;&nbsp;n;map&lt;int,&nbsp;int&gt;&nbsp;mp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;n;&nbsp;i&nbsp;++)&nbsp;{int&nbsp;x;cin&nbsp;&gt;&gt;&nbsp;x;mp[x]&nbsp;++;}for&nbsp;(int&nbsp;i&nbsp;=&nbsp;3;&nbsp;i&nbsp;&lt;=&nbsp;n;&nbsp;i&nbsp;++)&nbsp;{int&nbsp;res&nbsp;=&nbsp;0;for&nbsp;(auto&amp;&nbsp;it&nbsp;:&nbsp;mp)&nbsp;{res&nbsp;=&nbsp;(res&nbsp;+&nbsp;C(it.second,&nbsp;i))&nbsp;%&nbsp;p;}cout&nbsp;&lt;&lt;&nbsp;res&nbsp;&lt;&lt;&nbsp;'&nbsp;';}}3,最小生成树模板题,注意处理一下正边#include&nbsp;&lt;cmath&gt;#&nbsp;include&nbsp;&lt;iostream&gt;#include&nbsp;&lt;queue&gt;using&nbsp;namespace&nbsp;std;const&nbsp;int&nbsp;N&nbsp;=&nbsp;1e5&nbsp;+&nbsp;10;int&nbsp;p[N];int&nbsp;find(int&nbsp;x)&nbsp;{if&nbsp;(x&nbsp;!=&nbsp;p[x])&nbsp;p[x]&nbsp;=&nbsp;find(p[x]);return&nbsp;p[x];}int&nbsp;main()&nbsp;{int&nbsp;n,&nbsp;m;cin&nbsp;&gt;&gt;&nbsp;n&nbsp;&gt;&gt;&nbsp;m;priority_queue&lt;pair&lt;int,&nbsp;pair&lt;int,&nbsp;int&gt;&gt;&gt;&nbsp;pq;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;1;&nbsp;i&nbsp;&lt;=&nbsp;n;&nbsp;i&nbsp;++)&nbsp;p[i]&nbsp;=&nbsp;i;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;1&nbsp;;&nbsp;i&nbsp;&lt;=&nbsp;m;&nbsp;i&nbsp;++)&nbsp;{int&nbsp;u,&nbsp;v,&nbsp;x;cin&nbsp;&gt;&gt;&nbsp;u&nbsp;&gt;&gt;&nbsp;v&nbsp;&gt;&gt;&nbsp;x;pq.push({x,&nbsp;{u,&nbsp;v}});}long&nbsp;long&nbsp;res&nbsp;=&nbsp;0;while&nbsp;(pq.size()&nbsp;&gt;&nbsp;0)&nbsp;{auto&nbsp;t&nbsp;=&nbsp;pq.top();pq.pop();int&nbsp;u&nbsp;=&nbsp;t.second.first;int&nbsp;v&nbsp;=&nbsp;t.second.second;int&nbsp;x&nbsp;=&nbsp;t.first;if&nbsp;(x&nbsp;&gt;=&nbsp;0)&nbsp;{res&nbsp;+=&nbsp;x;p[find(u)]&nbsp;=&nbsp;find(v);}&nbsp;else&nbsp;if&nbsp;(find(u)&nbsp;!=&nbsp;find(v))&nbsp;{res&nbsp;+=&nbsp;x;p[find(u)]&nbsp;=&nbsp;find(v);}}cout&nbsp;&lt;&lt;&nbsp;res&nbsp;&lt;&lt;&nbsp;'\n';
投递饿了么等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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