题解 | #复数集合#

复数集合

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

#include<iostream>
#include<queue>
#include<string>
#include<cmath>
using namespace std;

struct complex 
{
	int shi;
	int fu;
	complex(int a, int b) :shi(a), fu(b) {}

	friend  bool operator <(complex a, complex b)
	{
		return pow(a.shi,2)+pow(a.fu,2) < pow(b.shi,2)+pow(b.fu,2);
	}
};

int main()
{
	int N;
	scanf("%d", &N);
	string str;
	priority_queue<complex>q;
	while (N--)
	{
		cin>>str;
		if (str == "Pop")
		{
			if (q.empty() == false)
			{
				complex c = q.top();
				q.pop();
				printf("%d+i%d\n", c.shi, c.fu);
				printf("SIZE = %d\n", q.size());
			}
			else
			{
				cout << "empty\n";
			}
		}
		else if (str == "Insert")
		{
			int a, b;
			scanf("%d+i%d", &a, &b);
			q.push(complex(a, b));
			printf("SIZE = %d\n", q.size());
		}
	}
}

全部评论

相关推荐

点赞 评论 收藏
分享
下北澤大天使:你是我见过最美的牛客女孩😍
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务