题解 | #复数集合#
复数集合
http://www.nowcoder.com/questionTerminal/abdd24fa839c414a9b83aa9c4ecd05cc
#include "cstdio"
#include "queue"
using namespace std;
#include "string"
struct complex{
int re;
int im;
};
bool operator < (complex lhs,complex rhs){
//lhs的模<rhs的模,左边是父亲,右边是孩子
return lhs.re*lhs.re+lhs.im*lhs.im <rhs.re*rhs.re+rhs.im*rhs.im;
}
int main(){
int n;
priority_queue<complex> queue;
scanf("%d",&n);
for (int i = 0; i < n; ++i) {
char action[30];
scanf("%s",action);
string action1=action;
if (action1=="Pop"){
if (queue.empty()){
printf("empty\n");
} else{
printf("%d+i%d\n",queue.top().re,queue.top().im);
queue.pop();
printf("SIZE = %d\n",queue.size());
}
} else if (action1 =="Insert"){
int re,im;
scanf("%d+i%d",&re,&im);//格式化读取
complex c;
c.re=re;
c.im=im;
queue.push(c);
printf("SIZE = %d\n",queue.size());
}
}
}