题解 | 复数集合
#include <bits/stdc++.h>
#include <queue>
using namespace std;
struct Complex{
int real;
int imag;
Complex(int a,int b):real(a),imag(b){}
bool operator < (Complex c) const{
return real*real+imag*imag <c.real*c.real +c.imag*c.imag;
}
};
int main(){
int n;
while(cin>>n){
priority_queue<Complex> p;
for(int i=0;i<n;i++){
string s;
cin>>s;
if(s=="Pop"){
if(p.empty())cout<<"empty"<<endl;
else {
cout<<p.top().real<<"+i"<<p.top().imag<<endl;
p.pop();
cout<<"SIZE = "<<p.size()<<endl;
}
}else {
int a,b;
scanf("%d+i%d",&a,&b);
p.push(Complex(a,b));
cout<<"SIZE = "<<p.size()<<endl;
}
}
}
}
本题需要使用优先队列实现,且要对小于号进行重定义,因为这是个结构体
查看15道真题和解析