题目描述
一个复数(x+iy)集合,两种操作作用在该集合上: 1、Pop 表示读出集合中复数模值最大的那个复数,如集合为空 输出 empty ,不为空就输出最大的那个复数并且从集合中删除那个复数,再输出集合的大小SIZE; 2 Insert a+ib 指令(a,b表示实部和虚部),将a+ib加入到集合中 ,输出集合的大小SIZE; 最开始要读入一个int n,表示接下来的n行每一行都是一条命令。
输入描述:
输入有多组数据。
每组输入一个n(1<=n<=1000),然后再输入n条指令。
输出描述:
根据指令输出结果。
模相等的输出b较小的复数。
a和b都是非负数。
示例1
输入
3
Pop
Insert 1+i2
Pop
输出
empty
SIZE = 1
1+i2
SIZE = 0
分析
处理字符串,关于字符串,cstring 和 string 是不一样的。用cin输入的字符串,就要用string库函数,输出也要用cout,是对应的;不然会出错。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include <iostream> #include <vector> #include <queue> #include <cmath> #include <set> #include <cstdio> #include <string> using namespace std;
class Node{ public: int a; int b; float mode; public: bool operator<(const Node& rhs) const{ return this->mode<rhs.mode; } };
int main() { int n; string t, c; priority_queue<Node> numQ; cin>>n; while(n--){ cin>>t; if(!t.compare("Pop")){ if(numQ.empty()){ cout<<"empty"<<endl; }else{ Node num = numQ.top(); numQ.pop(); cout<<num.a<<"+i"<<num.b<<endl; cout<<"SIZE = "<<numQ.size()<<endl; } }else{ Node num; int a =0, b =0; scanf("%d+i%d",&a,&b); num.a = a; num.b = b; num.mode = sqrt(a*a+b*b); numQ.push(num); cout<<"SIZE = "<<numQ.size()<<endl; } } return 0; }
|