题解 | #复数集合#c++优先队列

复数集合

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

#include <bits/stdc++.h>
using namespace std;
struct node {
    int a;
    int b;
};
bool operator<( node n1, node n2) {
    //n1的模小于n2的模,则交换
    return n1.a * n1.a + n1.b * n1.b < n2.a * n2.a + n2.b * n2.b;
}
int main() {
    int n;
    priority_queue<node> q;
    while (cin >> n) {
        for (int i = 0; i < n; i++) {
//          string s;
            char s1[30];
            cin >> s1;
            string s = s1;
            if (s == "Pop") {
                if (!q.empty()) {
                    cout << q.top().a << "+i" << q.top().b << endl;
                    q.pop();
                    cout << "SIZE = " << q.size() << endl;

                } else {
                    cout << "empty" << endl;
                }
            } else if (s == "Insert") {
                int a, b;
                scanf("%d+i%d", &a, &b);
                node c;
                c.a = a;
                c.b = b;
                q.push(c);
                cout << "SIZE = " << q.size() << endl;
            }
        }
    }
    return 0;
}

全部评论

相关推荐

06-17 21:57
门头沟学院 Java
白友:噗嗤,我发现有些人事就爱发这些,明明已读不回就行了,就是要恶心人
点赞 评论 收藏
分享
头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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