题目1178:复数集合

题目描述:

    一个复数(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都是非负数。

import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.lang.Comparable;
import java.util.PriorityQueue;

class Main
{
	public static final boolean DEBUG = false;
	
	static class Complex implements Comparable<Complex>
	{
		int x, y;
		
		public Complex(int x, int y) {
			this.x = x;
			this.y = y;
		}
		
		public int compareTo(Complex other) {
			int p = x * x + y * y;
			int q = other.x * other.x + other.y * other.y;
			
			if (p != q) return q - p;
			
			return y - other.y;
		}
	}
	
	public static void main(String[] args) throws IOException
	{
		BufferedReader cin;
		String s;
		
		if (DEBUG) {
			cin = new BufferedReader(new FileReader("d:\\OJ\\uva_in.txt"));
		} else {
			cin = new BufferedReader(new InputStreamReader(System.in));
		}
		
		while ((s = cin.readLine()) != null) {
			int n = Integer.valueOf(s);
			
			PriorityQueue<Complex> pq = new PriorityQueue<Complex>();
			
			for (int i = 0; i < n; i++) {
				s = cin.readLine();
				if ("Pop".compareTo(s) == 0) {
					if (pq.isEmpty()) {
						System.out.println("empty");
					} else {
						Complex tmp = pq.poll();
						System.out.println(tmp.x + "+i" + tmp.y);
						System.out.println("SIZE = " + pq.size());
					}
				} else {
					int start = s.indexOf(' ') + 1;
					int end = s.indexOf('+');
					int x = Integer.valueOf(s.substring(start, end));
					int y = Integer.valueOf(s.substring(end + 2));
					
					Complex tmp = new Complex(x, y);
					pq.add(tmp);
					System.out.println("SIZE = " + pq.size());
					
				}
			}
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值