2022年的520钻石争 新式六合彩答案

07新式六合彩

题目分析/解题思路

本题题意比较简单,我们按照要求找到“第r行和第c列中最接近幸运值” 的那个数字即可,实现时注意避开值为-1的元素,同时注意不要重复统计 (r:c)这个坐标。

代码:

#include 

using namespace std;

int main() {

    cin.tie(nullptr)->sync_with_stdio(false);

    int n, m;

    cin >> n >> m;

    vector> mat(n, vector(m));

    for (auto &row : mat)

        for (auto &x : row) cin >> x;

    int r, c, val;

    cin >> r >> c >> val;

    --r, --c;

    int diff = INT_MAX;

    vector> seq;

    for (int i = 0; i < m; ++i) {

        if (mat[r][i] == -1) continue;

        if (abs(mat[r][i] - val) > diff) continue;

        if (abs(mat[r][i] - val) < diff) seq.clear();

        diff = abs(mat[r][i] - val);

        seq.emplace_back(r, i);

    }

    for (int i = 0; i < n; ++i) {

        if (i == r) continue;

        if (mat[i][c] == -1) continue;

        if (abs(mat[i][c] - val) > diff) continue;

        if (abs(mat[i][c] - val) < diff) seq.clear();

        diff = abs(mat[i][c] - val);

        seq.emplace_back(i, c);

    }

    for (auto &[x, y] : seq)

        cout << "(" << x + 1 << ":" << y + 1 << ")" << '\n';

    return 0;

}

你可能感兴趣的:(蓝桥杯,c++,算法)