【机试备考】Day11-复数集合 | 运算符重载

题目

BUPT 2011 网研 ProblemB
一个复数(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都是非负数。

示例

输入

3
Pop
Insert 1+i2
Pop

输出

empty
SIZE = 1
1+i2
SIZE = 0

题解

复数的模= 实 部 的 平 方 + 虚 部 的 平 方 \sqrt{实部的平方+虚部的平方} +

法1:cin读入串+vector作容器

  1. Pop操作:利用了sort函数,自定义排序规则cmp对复数集合进行排序,最后一个元素即为最大的复数,用pop_back()直接删除,再输出vector的大小即可
  2. Insert操作:直接push_back(),再输出vector的大小

需要注意的是求复数的模时,截出的实部和虚部均为string类型,需要转成int

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//string类型数字转int型
int str2int(string a)
{
    
    
    int res=0;
    for(int i=0;i<a.length();i++)
    {
    
    
        res*=10;
        res+=a[i]-'0';
    }
    return res;
}
//自定义排序规则
bool cmp(string a,string b)
{
    
    
    //提取字符串中的实部和虚部,转换成int型
    string areal=a.substr(0,a.find('+'));
    int ar=str2int(areal);
    string breal=b.substr(0,b.find('+'));
    int br=str2int(breal);
    string aimg=a.substr(a.find('i')+1);
    int ai=str2int(aimg);
    string bimg=b.substr(b.find('i')+1);
    int bi=str2int(bimg);
    
    return ar*ar+ai*ai<br*br+bi*bi;
}
int main()
{
    
    
    int n;
    while(cin>>n)
    {
    
    
        vector<string>cmpx;//复数集合
        for(int i=0;i<n;i++)
        {
    
    
            string cmd;
            cin>>cmd;
            if(cmd=="Pop")
            {
    
    
                if(cmpx.size()==0)
                    cout<<"empty"<<endl;
                else
                {
    
    
                    //复数按从小到大排序,最后一个即为最大的复数
                    sort(cmpx.begin(),cmpx.end(),cmp);
                    cout<<cmpx[cmpx.size()-1]<<endl;
                    cmpx.pop_back();
                    cout<<"SIZE = "<<cmpx.size()<<endl;
                }
            }
            else if(cmd=="Insert")
            {
    
    
                string complex;
                cin>>complex;
                cmpx.push_back(complex);
                cout<<"SIZE = "<<cmpx.size()<<endl;
            }
        }
    }
}

法2:scanf读入数字+priority_queue作容器

  • 为什么要改?
    1.scanf 可以固定格式输入,这样就不用读入string以后人为分离实部和虚部,直接用scanf("%d+i%d",&a,&b)分别读入a,b即可
    【这道题强推!】
     
    2.优先级队列 priority_queue默认大顶堆,重载运算符之后直接输出堆顶.top()就是最大的复数,而不用每次调用sort函数
    【需要排序的题强推!】
#include<iostream>
#include<queue>
#include<cstdio>
#include<string>
using namespace std;
//复数用结构体表示更方便
struct complex
{
    
    
    int real;//实部
    int img;//虚部
    int val;//模
    complex(int r,int i){
    
    
        real=r;
        img=i;
        val=r*r+i*i;
    }
    bool operator < (complex c) const{
    
    
        return val<c.val;
    }
};
int main()
{
    
    
    int n;
    while(cin>>n)
    {
    
    
        priority_queue<complex>cmpx;//优先队列复数集合
        for(int i=0;i<n;i++)
        {
    
    
            string cmd;
            cin>>cmd;
            if(cmd=="Pop")
            {
    
    
                if(cmpx.size()==0)
                    cout<<"empty"<<endl;
                else
                {
    
    
                    //输出大顶堆堆顶,再删除堆顶
                    cout<<cmpx.top().real<<"+i"<<cmpx.top().img<<endl;
                    cmpx.pop();
                    cout<<"SIZE = "<<cmpx.size()<<endl;
                }
            }
            else if(cmd=="Insert")
            {
    
    
                int a,b;
                scanf("%d+i%d",&a,&b);
                cmpx.push(complex(a,b));
                cout<<"SIZE = "<<cmpx.size()<<endl;
            }
        }
    }
}

这里 scanf 其实是帮了挺大的忙,省了一大堆字符串转数字的操作

priority_queue 倒是没太大用,因为即使用 vector 也就是多 sort 一句话的事儿,而且似乎priority_queue更占内存

然后,记得,及时用结构体,比如这道题,会让整个代码的结构更清晰,写起来也更方便

猜你喜欢

转载自blog.csdn.net/qq_43417265/article/details/113310241
今日推荐