题解 | #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;
}
全部评论

相关推荐

Noob1024:一笔传三代,人走笔还在
点赞 评论 收藏
分享
已老实求offer😫:有点像徐坤(没有冒犯的意思哈)
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务