题解 | #复数集合#

复数集合

https://www.nowcoder.com/practice/abdd24fa839c414a9b83aa9c4ecd05cc

优先队列,默认为大根堆,大根堆只能重载小于号

#include <iostream>
#include <queue>
#include "queue"
using namespace std;
struct Complex {
    int shibu;
    int xubu;
    Complex(int a, int b) {
        shibu = a;
        xubu = b;
    }
    bool operator< (Complex f) const{
        int a = shibu * shibu + xubu * xubu;
        int b = f.shibu * f.shibu + f.xubu * f.xubu;
        if (a == b) return f.xubu < xubu;//模相同,虚部更小的虚数在排序时更“大”
        else return a < b;
    }
};
int main() {
    int n;
    while (cin >> n) { // 注意 while 处理多个 case
        // cout << a + b << endl;
        priority_queue<Complex> myQueue;
        while (n--) {
            string code;
            cin >> code;
            if (code == "Pop") {
                if (myQueue.empty()) cout << "empty" << endl;
                else {
                    Complex out = myQueue.top();
                    printf("%d+i%d\n", out.shibu, out.xubu);
                    myQueue.pop();
                    cout << "SIZE = " << myQueue.size() << endl;
                }
            } else {
                int a, b;
                scanf("%d+i%d", &a, &b);
                myQueue.push(Complex(a, b));
                cout << "SIZE = " << myQueue.size() << endl;
            }
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

06-02 15:17
门头沟学院 Java
心爱的idea:怎么会呢 应该是打招呼有问题 问就说实习6个月全国可飞随时到岗
点赞 评论 收藏
分享
牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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