题解 | #复数集合#
复数集合
https://www.nowcoder.com/practice/abdd24fa839c414a9b83aa9c4ecd05cc
#include <iostream> #include <queue> #include <cstring> using namespace std; struct Element { int value1; int value2; }; bool operator<(Element l, Element r) { return (l.value1 * l.value1 + l.value2 * l.value2) < (r.value1 * r.value1 + r.value2 * r.value2); } int func(string str) { int sum = 0; for (int i = 0; i < str.size(); i++) { sum = sum * 10 + (str[i] - '0'); } return sum; } int main() { int n; while (scanf("%d", &n) != EOF) { getchar(); priority_queue<Element> pqueue; for (int i = 0; i < n; i++) { string str; getline(cin, str); if (str == "Pop") { if (pqueue.empty()) { printf("empty\n"); } else { printf("%d+i%d\n", pqueue.top().value1, pqueue.top().value2); pqueue.pop(); printf("SIZE = %d\n", pqueue.size()); } } else { string t1, t2; int pos1 = str.find(" "); int pos2 = str.find("i"); t1 = str.substr(pos1 + 1, pos2 - pos1 - 2); t2 = str.substr(pos2 + 1); // cout << t1 << " " << t2 << endl; Element e; e.value1 = func(t1); e.value2 = func(t2); pqueue.push(e); printf("SIZE = %d\n", pqueue.size()); } } } return 0; }