首页 > 编程语言 > 详细

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

时间:2020-03-12 20:15:37      阅读:73      评论:0      收藏:0      [点我收藏+]

基本思想:

优先队列问题,但是也可以直接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;
			}
		}
	}
}

  

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

原文:https://www.cnblogs.com/songlinxuan/p/12482087.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!