//输出最大的可想到大根堆,所以采用优先队列,又是复数,重载'<'(小于号)即可。 #include "stdio.h" #include "queue" #include "string" using namespace std; struct plural{ int real; int fake; }; bool operator <(plural lhs,plural rhs){//小于号重载 int module1 = lhs.real*lhs.real + lhs.fake*lhs.fake; int module2 = rhs.real*rhs.real + rhs.fake*rhs.fake; if (module1 < module2) return true; else if (module1 == module2){ if (lhs.fake > rhs.fake) return true; else return false; } else{ return false; } } void myPQueuePop(priority_queue<plural> &myPQueue){//复数的弹出 if (myPQueue.empty()){ printf("empty\n"); } else{ plural e = myPQueue.top(); printf("%d+i%d\n",e.real,e.fake); myPQueue.pop(); printf("SIZE = %d\n",myPQueue.size()); } } void insertPlural(priority_queue<plural> &myPQueue,plural e){//复数的插入 myPQueue.push(e); printf("SIZE = %d\n",myPQueue.size()); } plural findE(string str){//识别出复数 int sum1 = 0;int sum2 = 0;//sum1为real,sum2为fake int i = 7; while (str[i] >= '0' && str[i] <= '9'){ sum1 = sum1*10+str[i]-'0'; ++i; } i = str.find('i'); ++i; while (str[i] >= '0' && str[i] <= '9'){ sum2 = sum2*10+str[i]-'0'; ++i; } plural e; e.real = sum1;e.fake = sum2; return e; } int main(){ int n;char buf1[100]; priority_queue<plural> myPQueue; scanf("%d\n",&n); for (int i = 0; i < n; ++i) { fgets(buf1,30,stdin); string str = buf1;str.pop_back(); if (str == "Pop"){ myPQueuePop(myPQueue); } else{ plural e = findE(str); insertPlural(myPQueue,e); } str.clear(); } }