题解 | #复数集合#
复数集合
http://www.nowcoder.com/practice/abdd24fa839c414a9b83aa9c4ecd05cc
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
//定义复数结构体
struct Complex {
int real; //实部
int imag; //虚部
Complex() {}
Complex(int a,int b):real(a),imag(b) {}
bool operator< (Complex c) const { //重载小于号
if(real * real + imag * imag == c.real * c.real + c.imag * c.imag) { //模相等,返回虚部小的,虚部越小,优先级越高
return imag > c.imag;
}
return real * real + imag * imag < c.real * c.real + c.imag * c.imag;
}
};
int main() {
int n;
while(scanf("%d",&n) != EOF) {
priority_queue<Complex> myPriorityQueue;
while(n--) {
string str;
cin >> str;
if(str == "Pop") {
if(!myPriorityQueue.empty()) {
Complex current = myPriorityQueue.top();
printf("%d+i%d\n",current.real,current.imag);
myPriorityQueue.pop();
printf("SIZE = %d\n",myPriorityQueue.size());
} else {
printf("empty\n");
}
} else{
int a;
int b;
scanf("%d+i%d",&a,&b);
myPriorityQueue.push(Complex(a,b));
printf("SIZE = %d\n",myPriorityQueue.size());
}
}
}
return 0;
}