当前位置: 首页 > 面试经验 >

小红书 5.7 C++ 3道笔试题 AK

优质
小牛编辑
78浏览
2023-05-07

小红书 5.7 C++ 3道笔试题 AK

第一题:

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main()
{
    long long A, B, M;
    cin >> A >> B >> M;
    vector<long long> num(50005, 0);
    num[0] = 1;
    num[1] = 1;
    int tmp_max = 1;
    int n;
    cin >> n;
    while (n--)
    {
        int q;
        cin >> q;
        for (int i = tmp_max; i < q; i++)
        {
            num[i + 1] = (A * num[i] + B * num[i - 1]) % M;
        }
        tmp_max = max(tmp_max, q);
        cout << num[q] << " ";
    }
    return 0;
}

第二题:

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main()
{
    int n, k;
    cin >> n >> k;
    vector<int> num(n);
    int tmp_min = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> num[i];
        tmp_min = max(num[i], tmp_min);
    }
    if (k == 1)
    {
        cout << max(num[0], num[n - 1]) << endl;
    }
    else
    {
        cout << tmp_min << endl;
    }
    return 0;
}

第三题:

#include <iostream>
#include <vector>
#include <cmath>
#include <set>
using namespace std;
int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        vector<set<int>> totol_bag;
        vector<int> count_ball(n, 0);
        set<int> all_color;
        bool res = true;
        for (int i = 0; i < n; i++)
        {
            cin >> count_ball[i];
            // 一个袋子中球的颜色
            set<int> ball;
            for (int j = 0; j < count_ball[i]; j++)
            {
                int color;
                cin >> color;
                ball.insert(color);
                all_color.insert(color);
            }
            totol_bag.push_back(ball);
            // 1.判断两两是否相同
            if (count_ball[i] != ball.size())
                res = false;
        }
        // 2.判断袋子中球的数目是否相同
        if (res)
        {
            for (int i = 1; i < n; i++)
            {
                if (count_ball[i] != count_ball[i - 1])
                {
                    res = false;
                    break;
                }
            }
        }
        // 3.判断每一种颜色在袋子中的数量
        vector<int> diff_color;
        if (res)
        {
            for (auto color = all_color.begin(); res && color != all_color.end(); color++)
            {
                int times = 0;
                for (int i = 0; i < n; i++)
                {
                    if (totol_bag[i].find(*color) != totol_bag[i].end())
                    {
                        times++;
                    }
                }
                if (times == n)
                {
                    diff_color.push_back(*color);
                }
                else if (times != 1)
                {
                    res = false;
                }
            }
        }
        if (res == false)
        {
            cout << "No" << endl;
        }
        else
        {
            cout << "Yes";
            if (diff_color.size() != 0)
            {
                for (auto i : diff_color)
                {
                    cout << " " << i;
                }
            }
            cout << endl;
        }
    }
    return 0;
}

待会发第二题的证明

#小红书信息集散地##实习##小红书笔试#
 类似资料: