利用scanf("%d+i%d)的方法来实现特殊形式的输入 #include <iostream> #include<queue> using namespace std; struct num{ int real; //实部 int fiction; //虚部 }; bool operator < (num lhs,num rhs) { if (lhs.fiction * lhs.fiction + lhs.real * lhs.real < rhs.fiction * rhs.fiction + rhs.real * rhs.real) return true; else if (lhs.fiction * lhs.fiction + lhs.real * lhs.real == rhs.fiction * rhs.fiction + rhs.real * rhs.real) { return lhs.fiction > rhs.fiction; } else { return false; } } int main() { //利用优先队列,即大根堆来解题 int n; string str; while (cin >> n) { priority_queue<num> queue; int a, b; for (int i = 0; i < n; i++) { cin >> str; if (str == "Pop") { if (queue.empty()) { cout << "empty" << endl; } else { cout << queue.top().real << "+i" << queue.top().fiction << endl; //输出结果 queue.pop(); //删除复数 cout << "SIZE = " << queue.size() << endl; } } else { num n; scanf("%d+i%d", &a, &b); n.real = a; n.fiction = b; queue.push(n); cout << "SIZE = " << queue.size() << endl; } } } }