复数的集合

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SDUTyangkun/article/details/87968272

链接:https://www.nowcoder.com/questionTerminal/abdd24fa839c414a9b83aa9c4ecd05cc?toCommentId=2580370
来源:牛客网

一个复数(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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
    int x;
    int y;
    int mod;
}s[1100];
int cmp(node a, node b)
{
    if(a.mod == b.mod)
        return a.y > b.y;
    return a.mod < b.mod;
}
int main()
{
    int n;
    scanf("%d", &n);
     int top = 0;
    while(n--)
    {

        char str1[10],str2[20];
        scanf("%s", str1);
        if(str1[0] == 'P')
        {
            if(top == 0)
            {
                printf("empty\n");continue;
            }
            else
            {
               printf("%d+i%d\n",s[top-1].x,s[top-1].y);
               printf("SIZE = %d\n",--top);
            }

        }
        else
        {
            scanf("%s",str2);
            int num1 = 0;
            int cnt = 0;
            for(int i = 0; str2[i] != '+'; i++)
            {
                num1 = num1*10+(str2[i] - '0'),cnt++;
            }

            int num2 = 0;
            for(int i = cnt + 2; i < strlen(str2); i++)
                 num2 = num2*10+(str2[i] - '0');
            s[top].x = num1;
            s[top].y = num2;
            s[top++].mod = num1*num1 + num2*num2;
            sort(s,s+top,cmp);
            printf("SIZE = %d\n",top);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/SDUTyangkun/article/details/87968272
今日推荐