题解 | #复数集合#

复数集合

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

#include<iostream>
#include<cstdio>
#include<queue>

using namespace std;


//定义复数结构体
struct Complex {
	int real;    //实部
	int imag;     //虚部
	Complex() {}
	Complex(int a,int b):real(a),imag(b) {}
	bool operator< (Complex c) const {    //重载小于号
		if(real * real + imag * imag == c.real * c.real + c.imag * c.imag) {  //模相等,返回虚部小的,虚部越小,优先级越高
			return imag > c.imag;
		}
		return real * real + imag * imag < c.real * c.real + c.imag * c.imag;
	}
};


int main() {
	int n;
	while(scanf("%d",&n) != EOF) {
		priority_queue<Complex> myPriorityQueue;
		while(n--) {
			string str;
			cin >> str;
			if(str == "Pop") {
				if(!myPriorityQueue.empty()) {
					Complex current = myPriorityQueue.top();
					printf("%d+i%d\n",current.real,current.imag);
					myPriorityQueue.pop();
					printf("SIZE = %d\n",myPriorityQueue.size());
				} else {
					printf("empty\n");
				}
			} else{
				int a;
				int b;
				scanf("%d+i%d",&a,&b);
				myPriorityQueue.push(Complex(a,b));
				printf("SIZE = %d\n",myPriorityQueue.size());
			}
		}
	}
	return 0;
}
全部评论

相关推荐

10-30 23:23
已编辑
中山大学 Web前端
去B座二楼砸水泥地:这无论是个人素质还是专业素质都👇拉满了吧
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务