Test 7: check for dependence on either compareTo() or compare()
returning { -1, +1, 0 } instead of { negative integer,
positive integer, zero }
* filename = equidistant.txt
- numberof entries in student solution: 0
- numberof entries inreference solution: 4
- 4 missing entries in student solution, including: '(30000, 0) -> (20000, 10000) -> (10000, 20000) -> (0, 30000)'
==> FAILED
week3课件中,递归调用的图示: 这是包含两个递归调用的递归 (图示只画了一半)
代码
(如需提交,请删除中文注释)
一:Point.java
import java.util.Comparator;
import edu.princeton.cs.algs4.StdDraw;
publicclass Point implements Comparable<Point> {
// x-coordinate of this pointprivate final int x;
// y-coordinate of this pointprivate final int y;
// constructs the point (x, y)publicPoint(int x, int y) {
this.x = x;
this.y = y;
}
// draws this pointpublicvoiddraw() {
StdDraw.point(x,y);
}
// draws the line segment from this point to that pointpublicvoiddrawTo(Point that) {
StdDraw.line(this.x, this.y, that.x, that.y);
}
// string representationpublic String toString() {
return"(" + x + ", " + y + ")";
}
// compare two points by y-coordinates, breaking ties by x-coordinates publicintcompareTo(Point that) {
if(y<that.y || (y==that.y && x<that.x)) return -1;
elseif(y==that.y && x==that.x) return0;
elsereturn +1;
}
// the slope between this point and that pointpublicdoubleslopeTo(Point that) {
if(x==that.x && y==that.y) return Double.NEGATIVE_INFINITY;
if(x==that.x && y!=that.y) return Double.POSITIVE_INFINITY;
if(y==that.y)