题解 | #复数集合#
复数集合
https://www.nowcoder.com/practice/abdd24fa839c414a9b83aa9c4ecd05cc
//当涉及到多次删除等操作,还是要用队列或栈之类,而不是用vector //若对输出有自定义优先级要求,则用优先队列 #include <queue> #include <string> #include <iostream> using namespace std; struct Complex{ int real; int imag; Complex(int a,int b): real(a),imag(b){} bool operator< (Complex x) const { //重载小于号 return real*real+imag*imag<x.real*x.real+x.imag*x.imag; } }; int main(){ int n; while(scanf("%d",&n)!=EOF){ priority_queue<Complex> myQueue; while(n--){ string str; cin>>str; if(str=="Pop"){ if(myQueue.empty()) printf("empty\n"); else{ Complex a=myQueue.top(); myQueue.pop(); printf("%d+i%d\n",a.real,a.imag); printf("SIZE = %d\n",myQueue.size()); } }else{ int a,b; scanf("%d+i%d",&a,&b); //scanf的这个操作类似于正则匹配 myQueue.push(Complex(a,b)); printf("SIZE = %d\n",myQueue.size()); } } } return 0; }