算法 练习1.2

1.2.1^
^(表示待优化的程序)

import edu.princeton.cs.algs4.Counter;
import edu.princeton.cs.algs4.Interval1D;
import edu.princeton.cs.algs4.Interval2D;
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.StdOut;


public class Exercise1_2_1 {
   
    public static void main(String[] args){

        int N = Integer.parseInt(args[0]);
        Interval1D xinterval = new Interval1D(.1,.9);//这里的面积为0.64
        Interval1D yinterval = new Interval1D(.1,.9);
        Interval2D box = new Interval2D(xinterval,yinterval);
        box.draw();



        double[][] poly = new double[N][2];
        Counter c = new Counter("hits");
        for(int i = 0;i < N;i++){
            double x = Math.random();
            double y = Math.random();

            Point2D p = new Point2D(x,y);

            poly[i][0] = x;
            poly[i][1] = y;


            if(!box.contains(p))
                c.increment();
            else
                p.draw();
        }


        double[] d = new double[N];
        for(int i = 0;i < N - 1;i++){
            for(int j = i + 1;j < N;j++){
                d[i] = (poly[i][0] - poly[j][0]) * (poly[i][0] - poly[j][0]) + (poly[i][1] - poly[j][1]) * (poly[i][1] - poly[j][1]); 

                }
    }


        double m =  d[0];
        for(int i = 0; i < N - 1;i++){
            //StdOut.println(d[i]);
            if(m > d[i])
                m = d[i];
        }



        StdOut.println(Math.sqrt(m));
        StdOut.println(c);
        StdOut.println(box.area());
    }
}

1.2.2

import edu.princeton.cs.algs4.Interval1D;
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class Exercise1_2_2{
   
    public static void main(String[] args){
        int N = Integer.parseInt(args[0]);
        Interval1D[] interval = new Interval1D[N];
        double[] d = StdIn.readDoubles();
        for(int i = 0;i < N;i++){
            interval[i] = new Interval1D(d[2 * i],d[2 * i + 1]);
            for(int j = N / 2;j < N;j++){
                if(i != j)
                    if(interval[i].intersects(interval[j])){
                        StdDraw.line(interval[i].left(),0.5,interval[i].right(),0.5);
                        StdOut.println("Interval[" + i + "] intersects with Interval[" + j + "]");
                    }
            }
        }
    }
}

1.2.3

import edu.princeton.cs.algs4.Interval1D;
import edu.princeton.cs.algs4.Interval2D;
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;

public class Exercise1_2_3{
    public static void main(String[] args){
        StdOut.println("Enter N: ");
        int N = Integer.parseInt(args[0]);
        StdOut.println("Enter min and max: ");
        double min = Double.parseDouble(args[1]);
        double max = Double.parseDouble(args[2]);
        StdDraw.setXscale(0,1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值