复数集合

题目描述

    一个复数(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

此题涉及到的知识点有:

1.类的重载运算符

2.类在优先队列中的使用

3.对const成员函数的一点理解

对这些的讲解在写在另外一篇文章里,这里只贴出此题代码:

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;

class Complex
{
public:
    double Re;
    double Im;
    double mo;
    void Input()
    {
        scanf("%lf+i%lf",&Re,&Im);
        mo = sqrt(Re*Re + Im*Im);
    }
    void Show() const
    {
        printf("%.0lf+i%.0lf\n",Re,Im);
    }

    bool operator < ( const Complex &c2) const
    {
        if(this->mo != c2.mo)
            return this->mo<c2.mo;
        else
            return this->Im>c2.Im;
    }
};
int main()
{
    int n,i;
    string temp;
    cin>>n;
    priority_queue<Complex,vector<Complex>,less<Complex> >q;
    string Pop = "Pop",Insert = "Insert";
    for(i=0;i<n;i++)
    {
        cin>>temp;
        if(temp == Insert)
        {
            Complex temp;
            temp.Input();
            q.push(temp);
            cout<<"SIZE = "<<q.size()<<endl;
        }
        else
        {
            if(q.empty())
                cout<<"empty"<<endl;
            else
            {
                q.top().Show();
                q.pop();
                cout<<"SIZE = "<<q.size()<<endl;
            }

        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值