0913招行信用卡【信用卡推荐用户列表】-数据方向编程题

信用卡推荐用户列表

(好像是这个名字)

我的思路

设计一个基于map的二叉树结构,然后求深度。我的代码:

#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;

int OutputBigClient(map<string, pair<string, string>> &r, int n, 
    string &head, vector<string> &output){

    if (head == "*")    return -1;
    if (r.find(head) == r.end()){
        if (n == 0)    output.push_back(head);
        return 0;
    }
    int ans = 2;
    ans += OutputBigClient(r, n, r[head].first, output);
    ans += OutputBigClient(r, n, r[head].second, output);
    if (ans >= n)
        output.push_back(head);
    return ans;
}
int main() {
    int m, n;
    cin >> m;
    cin >> n;
    map<string, pair<string, string>> rec_list;
    vector<string> output;
    string x, y, z;
    for (int i = 0; i < m; i++){
        cin >> x >> y >> z;
        rec_list[x] = make_pair(y, z);
    }
    x = "A";
    OutputBigClient(rec_list, n, x, output);
    if (!output.empty()){
        cout << output[0];
        for (int i = 1; i < output.size(); ++i){
            cout << " " << output[i];
        }
        cout << endl;
    }
    else
    {
        cout << "None" << endl;
    }
    //system("Pause");
    return 0;
}

100% 通过~


我以为可能会不止一个字母,所以用了string。提交了我才发现客户名称一定是大写字母。。。。


一开始我也是83.33%,后来发现我是把第一个出现的节点当成是根节点了。然而按题目要求,根节点一定是"A",改了之后就100%了,不知道大家是不是也是这个问题。

轻拍~谢谢

全部评论
终于明白为什么是83了
点赞 回复 分享
发布于 2017-09-14 01:16
这题的样例输出有点没看懂,为什么会出现E,根据输入E只推荐了H,I两个用户啊,没有超过2,为啥会被输出,求大佬解释下
点赞 回复 分享
发布于 2017-09-14 09:30
字节跳动
校招火热招聘中
官网直投
终于明白为什么是83了
点赞 回复 分享
发布于 2017-09-14 09:49

相关推荐

点赞 18 评论
分享
牛客网
牛客企业服务