zoukankan      html  css  js  c++  java
  • 14. 区间相交

     n个左右端点都为整数的区间,判断每个区间是否有与其它某个区间相交(区间端点重合也算相交)。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication11
    {
        class Program
        {
            static void Main(string[] args)
            {
                Interval a = new Interval(1, 4);
                Interval b = new Interval(6, 9);
                Interval c = new Interval(4, 5);
                Interval[] input = { a, b, c };
                bool[] result;
                Intersected(input, out result);
                foreach (var item in result)
                {
                    Console.WriteLine(item);
                }
            }
    
            static void Intersected(Interval[] input, out bool[] result)
            {
                result = new bool[input.Length];
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = false;
                }
    
                for (int i = 0; i < input.Length; i++)
                {
                    if (result[i]==false)
                    {
                        for (int j = i+1; j < input.Length; j++)
                        {
                            if ((input[i].start>=input[j].start&&input[i].start<=input[j].end)||(input[i].end>=input[j].start&&input[i].end<=input[j].end))
                            {
                                result[i] = true;
                                result[j] = true;
                            }
                        }
                    }
                }
            }
        }
    
        public struct Interval
        {
            public int start;
            public int end;
            public Interval(int x, int y)
            {
                start = x;
                end = y;
            }
        }
    }
    View Code
  • 相关阅读:
    CentOS 5.5和5.6 安装后的网络配置
    CentOS 5.5 系统安全配置
    printk: messages suppressed
    “找不到出路的”vb6.0
    用户控件的烦扰
    rman恢复
    oracle数据字典
    oracle自关联表的子删父变功能实现
    oracle自治事务
    oracle表空间更名
  • 原文地址:https://www.cnblogs.com/Ligeance/p/3538272.html
Copyright © 2011-2022 走看看