题解 | 复数集合

#include<iostream>
#include<queue>
using namespace std;
struct xy {
    int a;
    int b;
    string str;
};

bool operator<(xy x1, xy x2) {
    int t = x1.a * x1.a + x1.b * x1.b;
    int h = x2.a * x2.a + x2.b * x2.b;
    return t < h; //小于就发生交换,才为真
}
priority_queue<xy> q;
int main() {
    int n;
    cin >> n;
    cin.get();
    while (n--) {
        string s;
        getline(cin, s);
        if (s == "Pop") {
            if (q.empty()) {
                cout << "empty" << endl;
                continue;
            } else {
                xy h = q.top();
                q.pop();
                cout << h.str << endl;
                cout << "SIZE = " << q.size() << endl;
                continue;
            }
        }
        int g = 0, j = 0, flag = 0, bj;
        for (int i = 0; i < s.size(); i++) {
            if (flag == 0 && s[i] >= '0' && s[i] <= '9') {
                g = g * 10 + s[i] - '0';
            }
            if (s[i] == ' ')bj = i;
            if (s[i] == '+')flag = 1;
            if (flag == 1 && s[i] >= '0' && s[i] <= '9') {
                j = j * 10 + s[i] - '0';
            }
        }
        xy k;
        k.a = g, k.b = j;
        s.erase(0, bj + 1);
        k.str = s;
        q.push(k);
        cout << "SIZE = " << q.size() << endl;
    }
}

全部评论

相关推荐

05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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