检查 C# 中的重叠系列

发布于 2024-12-06 03:23:15 字数 550 浏览 1 评论 0原文

我正在制作一个应用程序,用户添加开始和结束来定义范围

条件是范围不应重叠:

如何检查数字范围是否不重叠,例如

  1. 范围1开始5结束15
  2. 范围2开始1结束4
  3. 范围3 Start 16 End 20
  4. Range 4 Start 2 End 4

所以 Range 4 使设置无效,我如何在 C# 中检查这一点。

此外,用户可以按任何顺序添加范围,如上例所示,整个系列应该不重叠。

感谢您的帮助建议。

问候, Sakshi

回答:

我提出的解决方案是否正确: 如果 start 和 end 是需要验证的范围,则

  1. start >startRange 且 start 小于 endRange
  2. end>startRange 且 end 小于 endRange

上述 2 个条件验证该系列重叠。

其中 startRange 和 endRange 是所有现有范围的开始和结束。

I am making an application where the user adds start and end to define a range

The condition is that the range should not overlap:

How to check whether a number range is not overlapping e.g.

  1. Range 1 Start 5 End 15
  2. Range 2 Start 1 End 4
  3. Range 3 Start 16 End 20
  4. Range 4 Start 2 End 4

So the Range 4 makes the set invalid, how do I check this in C#.

Further the user can add the range in any order as in the example above, the entire series should be non overlapping.

Thanks for the help suggestion.

Regards,
Sakshi

Answer:

I made the solution is it correct:
If start and end is the range which needs to be validated then

  1. start >startRange and start less than endRange
  2. end>startRange and end less than endRange

The above 2 condition validates that the series is overlapping.

Where startRange and endRange is the start and end for all existing ranges.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

榆西 2024-12-13 03:23:15

OP 建议进行两项检查来验证新范围不与现有范围重叠。然而,这两项检查并未涵盖所有可能性。例如,如果现有范围是 (4,10) 而新范围是 (2,12),则不会对其进行标记,因为它在现有范围的开始之前开始,并在现有范围之后结束。

相反,我建议采用以下方法:

if (newRangeStart <= existingRangeEnd && newRangeEnd >= existingRangeStart) {
    // we have an overlap
}

本质上,重叠范围有四种可能性:

  1. 在现有范围之前开始并在现有范围内结束的范围
  2. 在现有范围内开始并在其后结束的
  3. 范围 在现有范围内开始的范围现有范围并在现有范围内结束
  4. 在现有范围之前开始并在现有范围之后结束的范围

情况 (1) 和 (2) 包括部分重叠,而情况 (3) 和 (4) 包括完全重叠(无论是现有范围完全包含新范围 [情况3] 或新范围完全包围现有范围 [情况 4])。
OP 的代码捕获情况 1、2 和 3,但不捕获情况 4。此处的代码捕获重叠范围的所有 4 种可能性。

The OP suggests two checks to validate that a new range does not overlap with an existing range. However, these two checks do not cover all the possibilities. For instance, if the existing range is (4,10) and the new range is (2,12), it will not be flagged, because it starts prior to the start of the existing range, and ends afterwards.

Instead, I'd recommend the following approach:

if (newRangeStart <= existingRangeEnd && newRangeEnd >= existingRangeStart) {
    // we have an overlap
}

Essentially, there are four possibilities of overlapping ranges:

  1. A range which starts before the existing range and ends within the existing range
  2. A range which starts within the existing range and ends afterward
  3. A range which starts within the existing range and ends within the existing range
  4. A range which starts before the existing range and ends after the existing range

Cases (1) and (2) include partial overlap, while cases (3) and (4) include complete overlap (either the existing range completely encloses the new range [case 3] or the new range completely encloses the existing range [case 4]).
The OP's code catches cases 1, 2 and 3, but not case 4. The code here catches all 4 possibilities of an overlapping range.

妞丶爷亲个 2024-12-13 03:23:15

范围需要进行多次检查:

您可以有多种重叠变化,因此您需要进行多次测试。

|-----------|       |--------------|
    |------------------------|
         |-------------|
|--------------------------------|

首先检查:
范围开始 1 >= 范围 2 开始和范围 1 开始 <= 范围 2 结束

第二次检查:
范围 2 的起点 >= 范围 1 的起点和范围 2 的起点 <= 范围 1 的终点

第三次检查:
范围结束 1 >= 范围 2 开始且范围结束 1 <= 范围 2 结束

第四次检查:
范围结束 2 >= 范围 1 开始和范围结束 2 <= 范围 1 结束

这些检查假定两个范围上的结束 >= 开始。如果没有,您需要交换测试的开始和结束。

    public static bool DoRangesOverlap(int p_start1, int p_end1, int p_start2, int p_end2)
    {
        if ((p_start1 >= p_start2 && p_start1 <= p_end2) || (p_start2 >= p_start1 && p_start2 <= p_end1) || (p_end1 >= p_start2 && p_end1 <= p_end2) || (p_end2 >= p_start1 && p_end2 <= p_end1))
        {
            return true;
        }

        return false;
    }

Ranges need to have multiple checks:

You can have many variations of overlap, so you'll need to do several tests.

|-----------|       |--------------|
    |------------------------|
         |-------------|
|--------------------------------|

First Check:
Start of Range 1 >= Start of Range 2 and Start of Range 1 <= End of Range 2

Second Check:
Start of Range 2 >= Start of Range 1 and Start of Range 2 <= End of Range 1

Third Check:
End of Range 1 >= Start of Range 2 and End of Range 1 <= End of Range 2

Fourth Check:
End of Range 2 >= Start of Range 1 and End of Range 2 <= End of Range 1

These checks assume that End >= Start on both ranges. If not, you'll need to swap the start and end for the tests.

    public static bool DoRangesOverlap(int p_start1, int p_end1, int p_start2, int p_end2)
    {
        if ((p_start1 >= p_start2 && p_start1 <= p_end2) || (p_start2 >= p_start1 && p_start2 <= p_end1) || (p_end1 >= p_start2 && p_end1 <= p_end2) || (p_end2 >= p_start1 && p_end2 <= p_end1))
        {
            return true;
        }

        return false;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文