Algorithms 第一章1.2

1.2.1 编写一个Point2D实例,从命令行接受一个参数N。在单位正方形中生成N个随机点,然后计算两点之间的最近距离。
public class Test01 {

    public static void main(String[] args) {
        Random r = new Random();
        int N = Integer.parseInt(args[0]);
        Point2D[] arr = new Point2D[N];
        double x = -1, y = -1;
        int count = 0;
        while (count < 4) {
            x = r.nextDouble();
            y = r.nextDouble();
            if (!isExist(x, y, arr)) {
                Point2D point = new Point2D(x, y);
                arr[count++] = point;
            }
        }

        double min = distance(arr[0], arr[1]);
        for (int i = 0; i < N; i++)
            for (int j = i+1; j < N; j++) {
                double d = distance(arr[i], arr[j]);
                if (min > d) min = d;
            }
        System.out.println("minimum distance-->" + min);
    }

    private static boolean isExist(double x, double y, Point2D[] arr) {
        for (Point2D point : arr) {
            if (point != null && point.x == x && point.y == y) return true;
        }
        return false;
    }

    private static double distance(Point2D p1, Point2D p2) {
        return Math.sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
    }

    static class Point2D {
        double x;
        double y;

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

        @Override
        public String toString() {
            return "Point2D{" +
                    "x=" + x +
                    ", y=" + y +
                    '}';
        }
    }
}
1.2.2 编写一个Interval1D的用例,从命令行接受一个整数N。从标准输入中读取N个间隔(每个间隔由一对double值定义)并打印所有的相交的间隔对。
import edu.princeton.cs.algs4.Interval1D;
import edu.princeton.cs.algs4.StdIn;

public class Test02 {

    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);
        Interval1D[] ins = new Interval1D[N];
        while (!StdIn.isEmpty()) {
            for (int i = 0; i < ins.length; i++) {
                double min = StdIn.readDouble();
                double max = StdIn.readDouble();
                Interval1D in = new Interval1D(min, max);
                ins[i] = in;
            }
        }
        printInterval1D(ins);
    }

    private static void printInterval1D(Interval1D[] ins) {
        for (int i = 0; i < ins.length; i++) {
            for (int j = i+1; j < ins.length; j++) {
                if (ins[i].intersects(ins[j])) {
                    System.out.println(ins[i] + ", " + ins[j]);
                }
            }
        }
    }
}
1.2.3 编写一个Interval2D的用例,从命令行接受一个参数整数N、min和max。生成N个随机数2D间隔,其宽和高均匀地分布在单位正方形中的min和max之间。用StdDraw画出它们并打印出橡胶的间隔对的数量以及有包含关系的间隔对数量。
import edu.princeton.cs.algs4.*;

public class Test03 {

    public static void main(String[] args) {
        // 从命令行中接受一个整数参数N、min和max
        int N = Integer.parseInt(args[0]);
        double min = Double.parseDouble(args[1]);
        double max = Double.parseDouble(args[2]);
        Interval2D[] ins = new Interval2D[N];

        // [0, 10) new Random().nextInt(10)
        // [2, 10] new Random().nextInt(10 -2 + 1) + 2
        for (int i = 0; i < N; i++) {
            double x1 = StdRandom.uniform(min, max);
            double x2 = StdRandom.uniform(min, max);
            if (x1 >= x2) {
                double temp = x1;
                x1 = x2;
                x2 = temp;
            }

            double y1 = StdRandom.uniform(min, max);
            double y2 = StdRandom.uniform(min, max);
            if (y1 >= y2) {
                double temp = y1;
                y1 = y2;
                y2 = temp;
            }
            Interval1D x = new Interval1D(x1, x2);
            Interval1D y = new Interval1D(y1, y2);

            ins[i] = new Interval2D(x, y);
            ins[i].draw();
        }
        printInterval2DIntersects(ins);
    }

    private static void printInterval2DIntersects(Interval2D[] ins) {
        int containsCount = 0;
        int intersectsCount = 0;
        for (int i = 0; i < ins.length; i++) {
            for (int j = i+1; j < ins.length; j++) {
                if (ins[i].contains(ins[j]的图形的四个角的点)) containsCount++;
                if (ins[i].intersects(ins[j])) intersectsCount++;
            }
        }
        System.out.println("containsCount=" + containsCount);
        System.out.println("intersectsCount=" + intersectsCount);
    }
    
}
1.2.4 以下这段代码会打印什么?
String string1 = "hello";
String string2 = string1;
string1 = "world";
System.out.println(string1);
System.out.println(string2);

内存图
最后string1指向"world"字符串常量,string2指向"hello"。所以打印的是:

world
hello
1.2.6 如果字符串s中的字符循环移动任意位置之后能够得到另一个字符串t,那么是、就被称为t的回环边卫(circular rotation)。例如,ACTGACG就是TGACGAC的一个回环变位,反之亦然。判定这个条件的基因组序列的研究中是很重要的。编写一个程序检查两个给定的字符串s和t是否互为回环变位。提示:答案只需要一行indexOf()length()和字符串连接的代码。
public class Test06 {

    public static void main(String[] args) {
        String s = "ACTGACG";
        String t = "TGACGAC";
        String a = s + s;
        if (a.contains(t)) System.out.println("ok");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值