本文共 1474 字,大约阅读时间需要 4 分钟。
为了解决这个问题,我们需要根据给定的试机座位号,找出对应的考试座位号。我们可以使用哈希表来存储每个试机座位号对应的准考证号和考试座位号,这样查询时可以非常高效。
这种方法的时间复杂度是O(N + M),其中N是考生信息的数量,M是待查询的数量。由于N和M的范围都不大,这种方法非常高效。
#include#include #include using namespace std;int main() { int n; cin >> n; unordered_map > seat_map; for (int i = 0; i < n; ++i) { string line; getline(cin, line); size_t first_space = line.find(' '); size_t second_space = line.find(' ', first_space + 1); string z = line.substr(0, first_space); string s = line.substr(first_space + 1, second_space - first_space - 1); string k = line.substr(second_space + 1); int s_num = stoi(s); int k_num = stoi(k); seat_map[s_num] = make_pair(z, k_num); } int m; cin >> m; for (int i = 0; i < m; ++i) { string line; getline(cin, line); size_t pos = line.find(' '); string ss = line.substr(0, pos); int s_num = stoi(ss); auto it = seat_map.find(s_num); if (it != seat_map.end()) { string z = it->second.first; int k = it->second.second; cout << z << ' ' << k << endl; } } return 0;}
这种方法确保了高效查询和处理,能够快速解决问题。
转载地址:http://cxvfk.baihongyu.com/