编写一个Interval1D的用例,从命令行接受一个整数N。从标准输入中读取N个间隔(每个间隔由一对double值定义)并打印出所有相交的间隔对。
/**
* Description :
* Author : mn@furzoom.com
* Date : Sep 26, 2016 2:14:59 PM
* Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved.
*/
package com.furzoom.lab.algs.ch102;
import java.util.Arrays;
import edu.princeton.cs.algs4.Interval1D;
import edu.princeton.cs.algs4.StdIn;
/**
* ClassName : E10202 <br>
* Function : TODO ADD FUNCTION. <br>
* date : Sep 26, 2016 2:14:59 PM <br>
*
* @version
*/
public class E10202
{
public static void main(String[] args)
{
int n = Integer.parseInt(args[0]);
Interval1D[] intervals = new Interval1D[n];
for (int i = 0; i < n; i++)
{
double d1 = StdIn.readDouble();
double d2 = StdIn.readDouble();
intervals[i] = new Interval1D(d1, d2);
}
Arrays.sort(intervals, Interval1D.MIN_ENDPOINT_ORDER);
for (int i = 0; i < n; i++)
{
Interval1D interval = intervals[i];
for (int j = 0; j < n; j++)
{
if (j != i && interval.intersects(intervals[j]))
{
System.out.println(interval);
break;
}
}
}
}
}
E10202.txt
1.0 3.0
4.0 6.0
2.0 2.8
3.1 5.9
4.0 5.0
6.0 7.0
8.0 9.0
8.9 9.9
1.2 3.5
1.0 10.0
分别测试2、3、4、10对参数。
结果如下:
2对参数:
//null
3对参数:
[1.0, 3.0]
[2.0, 2.8]
4对参数:
[1.0, 3.0]
[2.0, 2.8]
[3.1, 5.9]
[4.0, 6.0]
10对参数:
[1.0, 3.0]
[1.0, 10.0]
[1.2, 3.5]
[2.0, 2.8]
[3.1, 5.9]
[4.0, 5.0]
[4.0, 6.0]
[6.0, 7.0]
[8.0, 9.0]
[8.9, 9.9]