题解 | #复数集合#
复数集合
https://www.nowcoder.com/practice/abdd24fa839c414a9b83aa9c4ecd05cc
优先队列,默认为大根堆,大根堆只能重载小于号
#include <iostream> #include <queue> #include "queue" using namespace std; struct Complex { int shibu; int xubu; Complex(int a, int b) { shibu = a; xubu = b; } bool operator< (Complex f) const{ int a = shibu * shibu + xubu * xubu; int b = f.shibu * f.shibu + f.xubu * f.xubu; if (a == b) return f.xubu < xubu;//模相同,虚部更小的虚数在排序时更“大” else return a < b; } }; int main() { int n; while (cin >> n) { // 注意 while 处理多个 case // cout << a + b << endl; priority_queue<Complex> myQueue; while (n--) { string code; cin >> code; if (code == "Pop") { if (myQueue.empty()) cout << "empty" << endl; else { Complex out = myQueue.top(); printf("%d+i%d\n", out.shibu, out.xubu); myQueue.pop(); cout << "SIZE = " << myQueue.size() << endl; } } else { int a, b; scanf("%d+i%d", &a, &b); myQueue.push(Complex(a, b)); cout << "SIZE = " << myQueue.size() << endl; } } } } // 64 位输出请用 printf("%lld")