题解 | #复数集合#

复数集合

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

#include<iostream>
#include<queue>
using namespace std;
struct cmp {
    bool operator ()(const pair<int, int> a, const pair<int, int> b) {
        if (a.first * a.first + a.second * a.second == b.first * b.first + b.second * b.second)
            return a.second < b.second;
        else if (a.first * a.first + a.second * a.second > b.first * b.first + b.second * b.second)
            return false;
        else
            return true;
    }
};
int main()
{
    int n;
    string str, num;
    while (cin >> n) {
        priority_queue<pair<int, int>, vector<pair<int, int>>, cmp > qu;
        for (int i = 0; i < n; ++i) {
            cin >> str;
            if (str == "Pop") {
                if (qu.empty()) {
                    cout << "empty" << endl;
                }
                else {
                    pair<int, int> t = qu.top();
                    cout << t.first << "+" << "i" << t.second << endl;
                    qu.pop();
                    cout << "SIZE = " << qu.size() << endl;
                }
            }
            else {
                cin >> num;
                int pos = num.find("+"),a = 0,b = 0;
                if (pos != string::npos) {
                    a = atoi(num.substr(0,pos).c_str());
                    b = atoi(num.substr(pos + 2, num.size() - 1 - pos).c_str());
                }
                qu.push(pair<int, int>(a,b));
                cout << "SIZE = " << qu.size() << endl;
            }
        }
    }
}
全部评论

相关推荐

剑桥断刀:找啥工作,牛客找个比如大厂软开或者随便啥的高薪牛马,大把没碰过妹子的技术仔,狠狠拿捏爆金币
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务