📜  通过移动线段的中心实现最大可能相交

📅  最后修改于: 2021-10-23 08:46:30             🧑  作者: Mango

给定 X 轴上的三个点,表示三个线段的中心。线段的长度也给定为 L。任务是将给定线段的中心移动 K 的距离,以最大化三条线之间的交点长度。
例子:

线段如下:

  1. 线段 1:(中心 1-L/2,中心 1+L/2)
  2. 线段 2:(Center2-L/2、Center2+L/2)
  3. 线段 3:(Center3-L/2,Center3+L/2)

方法:最初对中心进行排序,因为中间段永远不会移动,因为左右段总是可以到达其中心附近以增加交叉点长度。将有以下三种情况需要处理:

  • 情况一:当左右中心距离大于等于2*K+L时,此类场景交点将始终为零。没有重叠是可能的,因为即使在将两个中心移动 K 距离后,它们仍将相距 L 或更远的距离。
  • 情况二:当左右中心距离大于等于2*K时,存在一个交点等于(2*k-(center[2]-center[0]-length) ))可以使用简单的数学计算。
  • 情况 3:当左右中心之间的距离小于 2*K 时,这种情况下两个中心都可以与中间中心重合。因此,交点为L。

下面是上述方法的实现:

C++
#include 
using namespace std;
 
// Function to print the maximum intersection
int max_intersection(int* center, int length, int k)
{
    sort(center, center + 3);
 
    // Case 1
    if (center[2] - center[0] >= 2 * k + length) {
        return 0;
    }
 
    // Case 2
    else if (center[2] - center[0] >= 2 * k) {
        return (2 * k - (center[2] - center[0] - length));
    }
 
    // Case 3
    else
        return length;
}
 
// Driver Code
int main()
{
    int center[3] = { 1, 2, 3 };
    int L = 1;
    int K = 1;
    cout << max_intersection(center, L, K);
}


Java
// Java implementation
// of above approach
import java.util.*;
 
class GFG
{
 
// Function to print the
// maximum intersection
static int max_intersection(int center[],
                            int length, int k)
{
    Arrays.sort(center);
 
    // Case 1
    if (center[2] - center[0] >= 2 * k + length)
    {
        return 0;
    }
 
    // Case 2
    else if (center[2] - center[0] >= 2 * k)
    {
        return (2 * k - (center[2] -
                center[0] - length));
    }
 
    // Case 3
    else
        return length;
}
 
// Driver Code
public static void main(String args[])
{
    int center[] = { 1, 2, 3 };
    int L = 1;
    int K = 1;
    System.out.println( max_intersection(center, L, K));
}
}
 
// This code is contributed
// by Arnab Kundu


Python3
# Python3 implementation of above approach
 
# Function to print the maximum intersection
def max_intersection(center, length, k):
 
    center.sort();
 
    # Case 1
    if (center[2] - center[0] >= 2 * k + length):
        return 0;
 
    # Case 2
    elif (center[2] - center[0] >= 2 * k):
        return (2 * k - (center[2] - center[0] - length));
 
    # Case 3
    else:
        return length;
 
# Driver Code
center = [1, 2, 3];
L = 1;
K = 1;
print(max_intersection(center, L, K));
 
# This code is contributed
# by mits


C#
// C# implementation
// of above approach
using System;
 
class GFG
{
 
// Function to print the
// maximum intersection
static int max_intersection(int []center,
                            int length, int k)
{
    Array.Sort(center);
 
    // Case 1
    if (center[2] - center[0] >= 2 * k + length)
    {
        return 0;
    }
 
    // Case 2
    else if (center[2] -
             center[0] >= 2 * k)
    {
        return (2 * k - (center[2] -
                         center[0] - length));
    }
 
    // Case 3
    else
        return length;
}
 
// Driver Code
public static void Main()
{
    int []center = { 1, 2, 3 };
    int L = 1;
    int K = 1;
    Console.WriteLine(max_intersection(center, L, K));
}
}
 
// This code is contributed
// by Subhadeep Gupta


PHP
= 2 * $k + $length)
    {
        return 0;
    }
 
    // Case 2
    else if ($center[2] -
             $center[0] >= 2 * $k)
    {
        return (2 * $k - ($center[2] -
             $center[0] - $length));
    }
 
    // Case 3
    else
        return $length;
}
 
// Driver Code
$center = array(1, 2, 3);
$L = 1;
$K = 1;
echo max_intersection($center, $L, $K);
 
// This code is contributed
// by mits
?>


Javascript


输出:
1