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

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的指向互换,效率最高



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值