#include <iostream> #include<queue> #include<cstdio> #include<cstring> #include<string> #include<cmath>//求次幂,是pow,不是power //这题是假定a和b都是正整数吗? using namespace std; struct Complex { int a; int b; Complex(); Complex(int a0, int b0): a(a0), b(b0) {};//!!!一定一定一定要构造器在前面才行!! bool operator < (Complex c) const { //!!!输出b较小的值,默认输入大值,要倒转大于小于号 if (a * a + b * b == c.a * c.a + c.b * c.b ) { //模相等 return b > c.b; } else { return a * a + b * b < c.a * c.a + c.b * c.b; //模不相等,重新定义结构体的比较关系 } } // bool operator<(Complex c2) const { // if (pow(a, 2) + pow(b, 2) < pow(c2.a, 2) + pow(c2.b, 2)) { // return true; // } else if (pow(a, 2) + pow(b, 2) == pow(c2.a, 2) + pow(c2.b, 2)) { // cout<<(b < c2.b); // return b < c2.b; // } else { // return true; // } // } // 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) { // if (pow(c1.a, 2) + pow(c1.b, 2) < pow(c2.a, 2) + pow(c2.b, 2)) { // return true; // } else if (pow(c1.a, 2) + pow(c1.b, 2) == pow(c2.a, 2) + pow(c2.b, 2)) { // return c1.b > c2.b;//!!输出b较小的值,默认输入大值,要倒转大于小于号 // } else { // return true; // } //} 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; }