牛客 2025 年七夕节比赛 D、E、F 题解

D. 以名定亲

本题考查程序设计基础。按照题意模拟即可。

对性能有极高要求的人,可以使用哈希表等数据结构减少程序运行时间。

参考代码 1(哈希表,C++):

#include <array>
#include <iostream>
#include <vector>

using namespace std;

const int M = 1'000'000;

bool used[M];

bool check(int i, int j, const vector<array<int, 5>>& men, const vector<array<int, 4>>& women)
{
    for (int t = 0; t < 5; ++t)
    {
        if (used[men[i][t]])
            return false;
        used[men[i][t]] = true;
    }
    for (int t = 0; t < 4; ++t)
    {
        if (used[women[j][t]])
            return false;
        used[women[j][t]] = true;
    }
    return true;
}

int main()
{
    int n1 = 0, n2 = 0;
    bool exist = false;
    cin >> n1 >> n2;
    vector<array<int, 5>> men(n1);
    for (int i = 0; i < n1; ++i)
        for (int t = 0; t < 5; ++t)
        {
            cin >> men[i][t];
            --men[i][t];
        }
    vector<array<int, 4>> women(n2);
    for (int j = 0; j < n2; ++j)
        for (int t = 0; t < 4; ++t)
        {
            cin >> women[j][t];
            --women[j][t];
        }
    for (int i = 0; i < n1; ++i)
        for (int j = 0; j < n2; ++j)
        {
            if (check(i, j, men, women))
            {
                exist = true;
                cout << i + 1 << ' ' << j + 1 << '\n';
            }
            for (int t = 0; t < 5; ++t)
                used[men[i][t]] = false;
            for (int t = 0; t < 4; ++t)
                used[women[j][t]] = false;
        }
    if (!exist)
        cout << "None\n";
}

参考代码 2(暴力,C++):

#include <array>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n1 = 0, n2 = 0;
    bool exist = false;
    cin >> n1 >> n2;
    vector<array<int, 5>> men(n1);
    for (int i = 0; i < n1; ++i)
        for (int t = 0; t < 5; ++t)
        {
            cin >> men[i][t];
            --men[i][t];
        }
    vector<array<int, 4>> women(n2);
    for (int j = 0; j < n2; ++j)
        for (int t = 0; t < 4; ++t)
        {
            cin >> women[j][t];
            --women[j][t];
        }
    for (int i = 0; i < n1; ++i)
        for (int j = 0; j < n2; ++j)
        {
            int buf[9]{};
            bool fail = false;
            buf[0] = men[i][0];
            buf[1] = men[i][1];
            buf[2] = men[i][2];
            buf[3] = men[i][3];
            buf[4] = men[i][4];
            buf[5] = women[j][0];
            buf[6] = women[j][1];
            buf[7] = women[j][2];
            buf[8] = women[j][3];
            for (int t1 = 0; t1 < 8; ++t1)
            {
                for (int t2 = t1 + 1; t2 < 9; ++t2)
                    if (buf[t1] == buf[t2])
                    {
                        fail = true;
                        break;
                    }
                if (fail)
                    break;
            }
            if (!fail)
            {
                exist = true;
                cout << i + 1 << ' ' << j + 1 << '\n';
            }
        }
    if (!exist)
        cout << "None\n";
}

E. 转角遇到爱

将爱心图案顺时针旋转 度后,你会看到 <3

因此,如果 ,则答案为 Yes,否则为 No

参考代码(C++):

#include <iostream>

using namespace std;

int main()
{
    int n = 0;
    cin >> n;
    while (n > 0)
    {
        int x = 0;
        cin >> x;
        if (x < 3)
            cout << "Yes";
        else
            cout << "No";
        cout << '\n';
        --n;
    }
}

F. 表白局

参考代码(C++):

#include <iostream>

using namespace std;

int main()
{
    cout << "I love you, Zaoly!\n";
}
全部评论
我了个爱心小于3
6 回复 分享
发布于 2025-08-29 21:15 江西
我了个爱心小于3
1 回复 分享
发布于 2025-08-29 22:10 江苏
6666
点赞 回复 分享
发布于 2025-08-29 22:36 河南

相关推荐

03-18 09:57
已编辑
重庆邮电大学 C++
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

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