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%了,不知道大家是不是也是这个问题。
轻拍~谢谢
