题解 | #KY196 复数集合#

复数集合

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

解法一

#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <math.h>
using namespace std;


struct cmp {
    pair<double, int> stringToValue(string s) {
        int pos1 = s.find("+i");
        string s1 = s.substr(0, pos1);
        string s2 = s.substr(pos1+2);
        int a = stoi(s1);
        int b = stoi(s2);
        double v = sqrt(a*a + b*b);
        return {v, b};
    }
    bool operator() (string s1, string s2) {
        pair<double, int> v1 = stringToValue(s1);
        pair<double, int> v2 = stringToValue(s2);
        if (v1.first == v2.first) return v1.second > v2.second;
        else return v1.first < v2.first;
    }
};

priority_queue<string, vector<string>, cmp> pq;

int main()
{
    int n;
    while (cin >> n) {
        while (n--) {
            string s;
            cin >> s;
            if (s == "Pop") {
                if (pq.size() == 0) {
                    cout << "empty" << endl; 
                }
                else {
                    string tmp = pq.top();
                    cout << tmp << endl;
                    pq.pop();
                    int size = pq.size();
                    cout << "SIZE = " << size << endl;                      
                }                                
            }
            if (s.find("Insert") != string::npos) {
                string numStr;
                cin >> numStr;
                pq.push(numStr);
                int size = pq.size();
                cout << "SIZE = " << size << endl;               
            }
        }
    }
    return 0;
}

解法二

#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <math.h>
using namespace std;

struct Complex {
    int real;
    int imag;
    Complex(int a, int b) : real(a), imag(b) { }
    friend bool operator < (const Complex& c1, const Complex& c2) {
        return pow(c1.real,2) + pow(c1.imag,2) < pow(c2.real,2) + pow(c2.imag,2);
    }
};

int main()
{
    priority_queue<Complex> pq;
    int n;
    while (cin >> n) {
        while (n--) {
            string s;
            cin >> s;
            if (s == "Pop") {
                if (pq.size() == 0) {
                    cout << "empty" << endl; 
                }
                else {
                    Complex tmp = pq.top();
                    printf("%d+i%d\n", tmp.real, tmp.imag);
                    pq.pop();
                    int size = pq.size();
                    cout << "SIZE = " << size << endl;                      
                }                                
            }
            if (s.find("Insert") != string::npos) {
                int a, b;
                scanf("%d+i%d", &a, &b);
                pq.push(Complex(a,b));
                int size = pq.size();
                cout << "SIZE = " << size << endl;               
            }
        }
    }
    return 0;
}
全部评论

相关推荐

03-19 18:27
已编辑
门头沟学院 C++
26学院本太难了,很多公司机筛就给我刷了。机会都难拿到如果是简历存在问题也欢迎拷打————————————————————分割线——————————————————————2026.3.4更新:发完贴之后,时不时投递又收到了不少的笔试/面试邀请。主要是之前投递简历出去之后基本上都是沉默状态,年后好转了不少timeline:2026.01.21&nbsp;文远知行笔试,半年多没刷算法题&nbsp;-&gt;挂&nbsp;(后续HR说春招可以重新安排笔试)2026.2.4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;小鹏汇天&nbsp;技术一面,第二周收到结果&nbsp;-&gt;挂2026.2.12&nbsp;&nbsp;&nbsp;大众Cariad代招&nbsp;技术二面&nbsp;-&gt;Offer2026.2.28&nbsp;&nbsp;&nbsp;多益网络技术面试,由于风评太差,一直在犹豫要不要接面试&nbsp;-&gt;推迟-----------分割线-----------2026.3&nbsp;月前的某一天,临时去电网报名了二批计算机岗位的笔试2026.3.6&nbsp;从上家公司实习离职,氛围最好的一家公司,leader&nbsp;说可以帮忙转正,但是流程太长,而且我们部门据说只有一个&nbsp;hc,更想要研究生,我很有可能是会被签外包公司在这里干活,就离职了。2026.3.9&nbsp;入职新公司,大众Cariad&nbsp;以外部公司的身份进组,项目组签了三年,后续三年应该都可以在这里呆,不知道有没有希望原地跳槽。2026.3.10&nbsp;电网考试居然说我通过资格审查了,短信约我去参加资格审查,请假一天,买了&nbsp;12&nbsp;号晚上的机票回成都2026.3.15&nbsp;参加国家电网三新计算机类的笔试2026.3.17&nbsp;电网出成绩了,感觉很低。觉得已经🈚️了2026.3.18&nbsp;收到电网面试通知,通知&nbsp;3.22-3.25&nbsp;这个时间去面试,我的岗位只招&nbsp;1&nbsp;个人。据说面试只有&nbsp;2-3&nbsp;人,不知道能不能成功
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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