题解 | #复数集合#

复数集合

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;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 12:11
点赞 评论 收藏
分享
酷酷我灵儿帅:这去不去和线不线下面说实话没啥关系
点赞 评论 收藏
分享
05-27 14:57
西北大学 golang
强大的社畜在走神:27届真不用急,可以搞点项目、竞赛再沉淀沉淀,我大二的时候还在天天打游戏呢
投递华为等公司10个岗位
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务