题解 | #复数集合#

复数集合

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

#include <iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>//求次幂,是pow,不是power
//这题是假定a和b都是正整数吗?
using namespace std;

typedef struct Complex {
    int a;
    int b;
    Complex();
    Complex(int a0, int b0): a(a0), b(b0) {};
    friend bool operator<(const Complex& c1, const Complex& c2);
    friend ostream& operator<<(ostream& out, const Complex& c);
};
Complex::Complex() {
}
//一般将双目运算符重载为友元函数
priority_queue<Complex> plur;
bool operator<(const Complex& c1, const Complex& c2) {
    bool tag=false;
    if (pow(c1.a, 2) + pow(c1.b, 2) < pow(c2.a, 2) + pow(c2.b, 2)) {
        tag=true;
    } else if (pow(c1.a, 2) + pow(c1.b, 2) == pow(c2.a, 2) + pow(c2.b, 2)) {
        tag=c1.b > c2.b;//!!输出b较小的
    }//不用再有else了
    return tag;
}

ostream& operator<<(ostream& out, const Complex& c) {
    out << c.a << "+i" << c.b;
    return  out;
}
int Insert(string str) {
    int pos = str.find('i');
    string stra = str.substr(0, pos - 1); //不包括第二个位置的数
    string strb = str.substr(pos + 1);
//    cout<<"a="<<stra<<"b="<<strb<<endl;
    int a = 0, b = 0;
    for (int i = 0; i < stra.size(); i++) {
        a = a * 10 + stra[i] - '0';

    }
//    cout<<a<<endl;
    for (int j = 0; j < strb.size(); j++) {
        b = b * 10 + strb[j] - '0';
    }
//    cout<<b<<endl;
    Complex s = Complex(a, b);
    plur.push(s);
    return plur.size();
}

string counterpart(string str) {
    string strRes;

    if (str[0] == 'P') {
        strRes = str;

    } else if (str[0] == 'I') {

        strRes = str.substr(7);

    }

    return strRes;
}

int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        cin.ignore();
        //!!!scanf函数或cin函数有回车符,
        //使用getline会导致直接跳过,所以要在这里加上cin.ignore()
        while (n > 0) {

            string str;
            getline(cin, str);
            string commend = counterpart(str);

            if (commend == "Pop") {
                if (plur.empty()) {
                    cout << "empty" << endl; //加endl换行
                } else {
                    Complex res = plur.top();
                    plur.pop();
                    cout << res << endl;;
                    cout << "SIZE = " << plur.size() << endl;

                }
            } else {
                cout << "SIZE = " << Insert(commend) << endl;
            }
            n--;
        }
    }
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
投递京东等公司10个岗位
点赞 评论 收藏
分享
来个厂收我吧:首先,市场侧求职我不是很懂。 但是,如果hr把这份简历给我,我会觉得求职人不适合做产品经理。 问题点: 1,简历的字体格式不统一,排版不尽如人意 2,重点不突出,建议参考star法则写个人经历 3,印尼官方货币名称为印度尼西亚卢比(IDR),且GMV690000印尼盾换算为305人民币,总成交额不高。 4,右上角的意向职位在发给其他公司时记得删除。 5,你所有的经历都是新媒体运营,但是你要投市场营销岗位,jd和简历不匹配,建议用AI+提示词,参照多个jd改一下经历内容。 修改建议: 1,统一字体(中文:思源黑体或微软雅黑,英文数字:time new romans),在word中通过表格进行排版(b站学) 2,校招个人经历权重:实习经历=创业经历(大创另算)>项目经历>实训经历>校园经历 3,请将项目经历时间顺序改为倒序,最新的放最上方。 4,求职方向不同,简历文字描述侧重点也需要不同。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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