-
题目描述:
-
一个复数(x+iy)集合,两种操作作用在该集合上:
1、Pop 表示读出集合中复数模值最大的那个复数,如集合为空 输出 empty ,不为空就输出最大的那个复数并且从集合中删除那个复数,再输出集合的大小SIZE;
2 Insert a+ib 指令(a,b表示实部和虚部),将a+ib加入到集合中 ,输出集合的大小SIZE;
最开始要读入一个int n,表示接下来的n行每一行都是一条命令。
-
输入:
-
输入有多组数据。
每组输入一个n(1<=n<=1000),然后再输入n条指令。
-
输出:
-
根据指令输出结果。
-
样例输入:
-
3 Pop Insert 1+i2 Pop
-
样例输出:
-
empty SIZE = 1 1+i2 SIZE = 0
-
提示:
-
模相等的输出b较小的复数。
a和b都是非负数。
-
来源:
- 2011年北京邮电大学网院研究生机试真题
-
#include <iostream> #include <stdio.h> #include <algorithm> #include <math.h> #include <string> #include <string.h> #include <vector> #include <queue> #include <stack> #include <sstream> using namespace std; struct node { int x; int y; int m; bool operator < (const node &a) const { if(m!=a.m) return m>a.m; else return y<a.y; } }; int n,a,b; node t; string s; vector<node> v; int main() { while(cin>>n) { v.clear(); while(n--) { cin>>s; if(s[0]=='P') { if(v.empty()) printf("empty\n"); else { printf("%d+i%d\n",v[0].x,v[0].y); v.erase(v.begin(),v.begin()+1); printf("SIZE = %d\n",v.size()); } } else if(s[0]=='I') { cin>>s; sscanf(s.c_str(),"%d+i%d",&t.x,&t.y); t.m=t.x*t.x+t.y*t.y; v.push_back(t); sort(v.begin(),v.end()); printf("SIZE = %d\n",v.size()); } } } return 0; }