题解 | #复数集合#

复数集合

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

全部评论

相关推荐

就在我现在公司的隔壁每天经过都唏嘘不已(就是羡慕)什么时候可以到这里上班啊
柯基在debug:从大学毕业投简历到现在了,应届的时候我都面到终面了,现在工作四年了连简历初筛都过不了了
投递莉莉丝游戏等公司8个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 18:35
简历上把1个月实习写成了3个月,会进行背调吗?
码农索隆:一个月有一个月的实习经历,三个月有三个月的实习经历
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

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