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; } } }