java 类内Comparable

package exercise;

import java.util.*;
class Spot implements Comparable{
	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();
	}
}

 

你可能感兴趣的:(java 类内Comparable)