题解 | #复数集合#

复数集合

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

#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

struct Element {
    int value1;
    int value2;
};

bool operator<(Element l, Element r) {
    return (l.value1 * l.value1 + l.value2 * l.value2) < (r.value1 * r.value1 + r.value2 * r.value2);
}

int func(string str) {
    int sum = 0;
    for (int i = 0; i < str.size(); i++) {
        sum = sum * 10 + (str[i] - '0');
    }
    return sum;
}

int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        getchar();
        priority_queue<Element> pqueue;
        for (int i = 0; i < n; i++) {
            string str;
            getline(cin, str);
            if (str == "Pop") {
                if (pqueue.empty()) {
                    printf("empty\n");
                } else {
                    printf("%d+i%d\n", pqueue.top().value1, pqueue.top().value2);
                    pqueue.pop();
                    printf("SIZE = %d\n", pqueue.size());
                }
            } else {
                string t1, t2;
                int pos1 = str.find(" ");
                int pos2 = str.find("i");
                t1 = str.substr(pos1 + 1, pos2 - pos1 - 2);
                t2 = str.substr(pos2 + 1);
               // cout << t1 << "  " << t2 << endl;
                Element e;
                e.value1 = func(t1);
                e.value2 = func(t2);
                pqueue.push(e);
                printf("SIZE = %d\n", pqueue.size());
            }

        }
    }

    return 0;
}

全部评论

相关推荐

11-22 16:49
已编辑
北京邮电大学 Java
美团 质效,测开 n*15.5
点赞 评论 收藏
分享
头像
11-07 01:12
重庆大学 Java
精致的小松鼠人狠话不多:签哪了哥
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务