您的位置:首页 > 其它

算法 Robert Sedgewick 习题答案 1.2 数据抽象

2013-11-25 21:17 190 查看
1.2..1

package chapter1_2;

public class Exercise1 {
public static void main(String[] args) {
int T = Integer.parseInt(args[0]);
Point[] points = new Point[T];
double min = Double.POSITIVE_INFINITY;

for (int i = 0; i < T; i++) {
Point p = new Point(Math.random(), Math.random());
points[i] = p;
}

for (int i = 0; i < T; i++)
for (int j = i + 1; j < T; j++) {
double distance = points[i].distance(points[j]);
System.out.println("点" + i + "点" + j + "之间距离为" + distance);
if (distance < min)
min = distance;

}
System.out.println("最近的两点距离为" + min);
}
}

class Point {
private double x;

public double getX() {
return x;
}

public double getY() {
return y;
}

private double y;

public Point(double x, double y) {
this.x = x;
this.y = y;
}

public double distance(Point p) {

return Math.sqrt(Math.pow(Math.abs(this.x - p.getX()), 2)
+ Math.pow(Math.abs(this.y - p.getY()), 2));

}

}


1.2.2.1.2.3

要调用它的库,麻烦无意义,略

1.2.4

world

hello

1.2.5

Hello World

1.2.6

package chapter1_2;

public class Exercise6 {

// TODO 采用了api中提供的函数indexOf,后期改写
public static boolean isCirRot(String a, String b) {
if (a.length() != b.length())
return false;
else
return (-1 != (a + a).indexOf(b));
}

public static void main(String[] args) {
boolean bool = isCirRot("aadsdew", "sdewaad");
System.out.println("两个字符串是回环变位:" + bool);
}
}

1.2.7

逆序输出字符串

1.2..8

将a,b的指向互换,效率最高
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐