|
1 | 1 | package exercise;
|
2 | 2 |
|
| 3 | +import edu.princeton.cs.algs4.Interval1D; |
| 4 | +import edu.princeton.cs.algs4.Interval2D; |
| 5 | +import edu.princeton.cs.algs4.StdDraw; |
| 6 | +import edu.princeton.cs.algs4.StdIn; |
3 | 7 | import edu.princeton.cs.algs4.StdOut;
|
| 8 | +import edu.princeton.cs.algs4.StdRandom; |
4 | 9 |
|
5 | 10 | public class E1_2_3 {
|
6 | 11 |
|
7 | 12 | public static void main(String[] args) {
|
8 | 13 | // TODO Auto-generated method stub
|
9 |
| - StdOut.print("agag"); |
| 14 | + int N = StdIn.readInt(); |
| 15 | + int min = StdIn.readInt(); |
| 16 | + int max = StdIn.readInt(); |
| 17 | + double[] number1 = new double[2]; |
| 18 | + double[] number2 = new double[2]; |
| 19 | + StdDraw.setXscale(min-1, max+1); |
| 20 | + StdDraw.setYscale(min-1, max+1); |
| 21 | + Interval1D X = new Interval1D(min,max); |
| 22 | + Interval1D Y = new Interval1D(min,max); |
| 23 | + Interval2D a = new Interval2D(X,Y); |
| 24 | + a.draw(); |
| 25 | + Interval2D[] Inter = new Interval2D[N]; |
| 26 | + for (int i = 0;i < N;i++) { |
| 27 | + for (int j = 0;j<2;j++) { |
| 28 | + number1[j] = StdRandom.uniform(min+1, max); |
| 29 | + number2[j] = StdRandom.uniform(min+1, max); |
| 30 | + if (number1[j] == number2[j]) |
| 31 | + number1[j] = number1[j]-1; |
| 32 | + StdOut.println(""+ number1[j]+" "+number2[j]); |
| 33 | + } |
| 34 | + Interval1D x= new Interval1D(Math.min(number1[0], number2[0]), Math.max(number1[0], number2[0])); |
| 35 | + Interval1D y= new Interval1D(Math.min(number1[1], number2[1]), Math.max(number1[1], number2[1])); |
| 36 | + Inter[i] = new Interval2D(x, y); |
| 37 | + Inter[i].draw(); |
| 38 | + } |
| 39 | + int cout=0; |
| 40 | + for (int i =0;i<Inter.length;i++) { |
| 41 | + for (int j = i+1 ;j<Inter.length;j++) { |
| 42 | + if(Inter[i].intersects(Inter[j])) |
| 43 | + { |
| 44 | + cout++; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + StdOut.println(cout); |
| 49 | + |
10 | 50 | }
|
11 | 51 |
|
| 52 | + |
12 | 53 | }
|
0 commit comments