北京邮电大学机试 复数集合 Easy *优先队列,起始排序也可以

基本思想:

优先队列问题,但是也可以直接sort cmp解决,感觉有点累赘,思想受限;

 

关键点:

无;

 

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

struct node {
	int x;
	int y;
};

struct cmp {
	bool operator () (node a, node b) {
		if ((pow(a.x, 2) + pow(a.y, 2)) == (pow(b.x, 2) + pow(b.y, 2))) {
			return a.y < b.y;
		}
		else
			return (pow(a.x, 2) + pow(a.y, 2)) < (pow(b.x, 2) + pow(b.y, 2));
	}
};

int trans(string s) {
	int cnt = 0;
	for (int i = 0; i < s.size(); i++) {
		cnt = cnt * 10 + (s[i] - '0');
	}
	return cnt;
}

int main() {
	int n;
	string s;
	while (cin >> n) {
		getchar();
		priority_queue<node, vector<node>, cmp>dp;
		for (int i = 0; i < n; i++) {
			getline(cin, s);
			if (s == "Pop") {
				if (!dp.empty()) {
					//如果不为空;
					cout<<dp.top().x<<"+i"<<dp.top().y<<endl;
					dp.pop();
					cout << "SIZE = " << dp.size() << endl;
				}
				else {
					//如果直接为空
					cout << "empty" << endl;
				}
			}
			else {
				//截取数值部分
				string str = s.substr(7, s.size() - 7);
				int index = -1;
				for (int j = 0; j < str.size(); j++) {
					if (str[j] == '+') {
						index = j;
						break;
					}
				}
				node no;
				no.x = trans(str.substr(0, index));
				no.y = trans(str.substr(index+2,str.size()-index-2));
				dp.push(no);
				cout << "SIZE = " << dp.size() << endl;
			}
		}
	}
}

  

posted @ 2020-03-12 19:20  暮云林凌  阅读(167)  评论(0)    收藏  举报