C++_二维数组(二维向量)字符串“[[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]]”_输入

2021/8/15 9:05:52

本文主要是介绍C++_二维数组(二维向量)字符串“[[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]]”_输入,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1、github打不开解决方法:

解决网址:https://www.jianshu.com/p/baf999efd45e
C:\Windows\System32\drivers\etc中的hosts文件,添加如下内容:
#github
140.82.112.4 github.com
199.232.69.194 github.global.ssl.fastly.net
185.199.108.153 assets-cdn.github.com
185.199.110.153 assets-cdn.github.com
185.199.111.153 assets-cdn.github.com
刷新DNS缓存
打开cmd窗口,执行ipconfig /flushdns命令

2、题目:1583. 统计不开心的朋友

给你一份 n 位朋友的亲近程度列表,其中 n 总是 偶数 。

对每位朋友 i,preferences[i] 包含一份 按亲近程度从高到低排列 的朋友列表。换句话说,排在列表前面的朋友与 i 的亲近程度比排在列表后面的朋友更高。每个列表中的朋友均以 0 到 n-1 之间的整数表示。

所有的朋友被分成几对,配对情况以列表 pairs 给出,其中 pairs[i] = [xi, yi] 表示 xi 与 yi 配对,且 yi 与 xi 配对。

但是,这样的配对情况可能会是其中部分朋友感到不开心。在 x 与 y 配对且 u 与 v 配对的情况下,如果同时满足下述两个条件,x 就会不开心:

x 与 u 的亲近程度胜过 x 与 y,且
u 与 x 的亲近程度胜过 u 与 v
返回 不开心的朋友的数目 。

 

示例 1:

输入:n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]]
输出:2
解释:
朋友 1 不开心,因为:
- 1 与 0 配对,但 1 与 3 的亲近程度比 1 与 0 高,且
- 3 与 1 的亲近程度比 3 与 2 高。
朋友 3 不开心,因为:
- 3 与 2 配对,但 3 与 1 的亲近程度比 3 与 2 高,且
- 1 与 3 的亲近程度比 1 与 0 高。
朋友 0 和 2 都是开心的。
示例 2:

输入:n = 2, preferences = [[1], [0]], pairs = [[1, 0]]
输出:0
解释:朋友 0 和 1 都开心。
示例 3:

输入:n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]]
输出:4

#include "vector"
#include "string"
#include "iostream"
// #include "nlohmann/json.hpp" // 目录要做个调整就可以这么导入??
#include ".\lib\json-3.9.1\single_include\nlohmann\json.hpp"
using json = nlohmann::json;
using namespace std;

class Solution {
public:
    int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
        vector<vector<int>> order(n, vector<int>(n));
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n - 1; ++j) {
                order[i][preferences[i][j]] = j;
            }
        }
        vector<int> match(n);
        for (const auto& pr: pairs) {
            match[pr[0]] = pr[1];
            match[pr[1]] = pr[0];
        }

        int unhappyCount = 0;
        for (int x = 0; x < n; ++x) {
            int y = match[x];
            int index = order[x][y];
            for (int i = 0; i < index; ++i) {
                int u = preferences[x][i];
                int v = match[u];
                if (order[u][x] < order[u][v]) {
                    ++unhappyCount;
                    break;
                }
            }
        }
        return unhappyCount;
    }
};

int main() {
    // 1、输入n:
    string nInput;
    getline(cin, nInput);
    int n = stoi(nInput);
    // cout << "n:" << n << endl;
    // 2、输入preferences:
    // string preferences = R"(
    //     [[1,2,3],[3,2,0],[3,1,0],[1,2,0]]
    // )";
    string preferences;
    getline(cin, preferences);
    auto preferencesJson = json::parse(preferences);
    // cout << "preferencesJson:" << preferencesJson << " " << preferencesJson.type_name() << endl;
    auto preferencesDump = preferencesJson.dump();
    // 3、输入pairs
    // [[1,2,3],[3,2,0],[3,1,0],[1,2,0]]
    // [[0,1],[2,3]]
    // string pairs = R"(
    //     [[0,1],[2,3]]
    // )";
    string pairs;
    getline(cin, pairs);
    auto pairsJson = json::parse(pairs);
    // cout << "pairsJson:" << pairsJson << " " << pairsJson.type_name() << endl;
    auto pairsDump = pairsJson.dump();
    vector<vector<int>> preferencesInt = preferencesJson;
    vector<vector<int>> pairsInt = pairsJson;
    // 调用函数
    cout << Solution().unhappyFriends(n, preferencesInt, pairsInt);

}
View Code

3、思路:字符串“[[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]]”转换为json格式,然后再赋值转换为向量(vector)

 



这篇关于C++_二维数组(二维向量)字符串“[[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]]”_输入的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程