算法练习(17):相交的间隔对(1.2.2)

本系列博客习题来自《算法(第四版)》,算是本人的读书笔记,如果有人在读这本书的,欢迎大家多多交流。为了方便讨论,本人新建了一个微信群(算法交流),想要加入的,请添加我的微信号:zhujinhui207407 谢谢。另外,本人的个人博客 http://www.kyson.cn 也在不停的更新中,欢迎一起讨论

算法(第4版)

知识点

  • Java创建对象
  • 间隔对
  • 判断间隔对相交

题目

1.2.2 编写一个Interval1D的用例,从命令行接受一个整数N.从标准输入中读取N个间隔(每个间隔由一对double值定义)并打印出所有相交的间隔对。


1.2.2 Write an Interval1D client that takes an int value N as command-line argu- ment, reads N intervals (each defined by a pair of double values) from standard input, and prints all pairs that intersect.

答案

public class Interval1D {
    
    private double lo;
    private double hi;
    
    public Interval1D (double tlo, double thi) {
        if (tlo > thi) {
            double tempDouble = tlo;
            tlo = thi;
            thi = tempDouble;
        }
        this.hi = thi;
        this.lo = tlo;
    }
    
    public double length(){
        return Math.abs(lo - hi);
    }
    
    public boolean contains(double x){
        return (x > lo) && (x < hi);
    }
    
    /***
     * 判断两个间隔是否相交
     * @param that
     * @return
     */
    public boolean intersect(Interval1D that) {
        if (this.hi < that.lo) {
            return false;
        }else if ((this.hi > that.lo) && (this.lo < that.hi)) {
            return true;
        }else {
            return false;
        }       
    }
    
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "( "+lo+", " + hi + " )";
    }
    

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ArrayList<Interval1D> interval1ds = new ArrayList<>();
        
        Interval1D interval1d1 = new Interval1D(3, 5);
        Interval1D interval1d2 = new Interval1D(4, 5);
        Interval1D interval1d3 = new Interval1D(1, 5);
        Interval1D interval1d4 = new Interval1D(7, 9);
        Interval1D interval1d5 = new Interval1D(1, 2);
        
        interval1ds.add(interval1d1);
        interval1ds.add(interval1d2);
        interval1ds.add(interval1d3);
        interval1ds.add(interval1d4);
        interval1ds.add(interval1d5);
        
        
        for (int i = 0; i < interval1ds.size(); i++) {
            
            for (int j = i + 1; j < interval1ds.size(); j++) {
                Interval1D tempInterval1d1 = interval1ds.get(i);
                Interval1D tempInterval1d2 = interval1ds.get(j);
                if (tempInterval1d1.intersect(tempInterval1d2)) {
                    System.out.println("intersect:"+tempInterval1d1+" " + tempInterval1d2);
                }
            }           
        }       
    }
}

打印出的结果:

intersect:( 3.0, 5.0 ) ( 4.0, 5.0 )
intersect:( 3.0, 5.0 ) ( 1.0, 5.0 )
intersect:( 4.0, 5.0 ) ( 1.0, 5.0 )
intersect:( 1.0, 5.0 ) ( 1.0, 2.0 )

代码索引

Interval1D.java

视频讲解

算法练习(17):相交的间隔对(1.2.2)

广告

我的首款个人开发的APP壁纸宝贝上线了,欢迎大家下载。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、实验目的 学习使用 weka 中的常用分类器,完成数据分类任务。 二、实验内容 了解 weka 中 explo...
    yigoh阅读 8,687评论 5 4
  • 首页 资讯 文章 资源 小组 相亲 登录 注册 首页 最新文章 IT 职场 前端 后端 移动端 数据库 运维 其他...
    Helen_Cat阅读 3,966评论 1 10
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,588评论 25 709
  • 早晨,为了锻炼身体,我们一帮男人,推车玩。丁老的身姿优美,我好像显得敷衍了事的样子。其实,我用尽了九牛二虎之力了。...
    阿牛老师阅读 606评论 1 5
  • 岐中总是睡得很晚 吊塔的灯会亮到夜里的十二点 于是它就睁着眼睛躺了下来 点上了一支十一块的磨砂猴 吸一口混着冬季的...
    你我有个草原阅读 176评论 0 0