java 类内Comparable

package exercise;

import java.util.*;
class Spot implements Comparable<Spot>{
	int x,y;
	double o;
	@Override
	public int compareTo(Spot p) {
		// TODO Auto-generated method stub
		double a=this.o-p.o;
		int b;
		if(a==0) b=0;
		else if (a>0) {
			b=1;
		}
		else b=-1;
		return b;
	}
	
}
public class Main{
	Scanner scan=new Scanner(System.in);
	int n,d;
	Spot[] spots=new Spot[1000+10];
	
	public Main() {
		super();
		int c=1;
		while(true) {
		n=scan.nextInt();
		d=scan.nextInt();
		if(n==0||d==0)
			break;
		for (int i = 0; i < n; i++) {
			spots[i]=new Spot();
		}
		boolean ok=true;
		for (int i = 0; i < n; i++) {
			spots[i].x=scan.nextInt();
			spots[i].y=scan.nextInt();
			spots[i].o=spots[i].x+Math.sqrt(d*d-spots[i].y*spots[i].y);
			if(d<=0||Math.abs(spots[i].y)>d) {
				ok=false;
			}
		}
		if(ok)
			mianProgram(c);
		else 
			System.out.println("Case "+c+": -1");
		}
		c++;
	}
	
	public void mianProgram(int c) {
		Arrays.sort(spots,0,n);
		int j=1;
		double ox=spots[0].o;
		for (int i = 0; i < n; i++) {
			double t=0;
			t=(ox-spots[i].x)*(ox-spots[i].x)+spots[i].y*spots[i].y;
			if(t>d*d) {
				j++;
				ox=spots[i].o;
				//System.out.println(ox);
			}
		}
		System.out.println("Case "+c+": "+j);
	}
	
	public static void main(String[] args) {
		new Main();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值