📌  相关文章
📜  查找磁铁之间的最大和最小距离

📅  最后修改于: 2021-04-26 18:25:00             🧑  作者: Mango

给定坐标平面中两个枢轴点(x0,y0)&(x1,y1)的坐标。沿着每个枢轴,分别借助于长度为r1和r2的字符串将两个不同的磁铁绑在一起。找出两个磁铁互相排斥时以及互相吸引时的距离。

例子 :

众所周知,磁体的特性是当它们面对相同的磁极时彼此排斥,而当它们面对相反的磁极彼此时彼此吸引。同样,吸引力和排斥力总是成一直线。

我们在坐标上有两个枢轴点,因此这些点之间的距离为D =((x1-x2) 2 +(y1-y2) 2 ) 1/2
同样,我们可以得出结论,在排斥时磁体之间的距离最大,并且枢轴之间的距离也应为两个字符串的长度之和。
如果有吸引力,我们有两种情况需要注意:
最小距离就是枢轴之间的距离–两根字符串的长度之和
否则,如果字符串的长度之和大于枢轴点之间的距离,则最小距离应为零。

借助图示的插图:

C++
// C++ program for max and min distance
#include 
using namespace std;
  
// Function for finding distance between pivots
int pivotDis(int x0, int y0, int x1, int y1)
{
    return sqrt((x1 - x0) * (x1 - x0) + 
                (y1 - y0) * (y1 - y0));
}
  
// Function for minimum distance
int minDis(int D, int r1, int r2)
{
    return max((D - r1 - r2), 0);
}
  
// Function for maximum distance
int maxDis(int D, int r1, int r2)
{
    return D + r1 + r2;
}
  
// Drivers code
int main()
{
    int x0 = 0, y0 = 0, x1 = 8, y1 = 0, r1 = 4, r2 = 5;
    int D = pivotDis(x0, y0, x1, y1);
    cout << "Distance while repulsion = " << maxDis(D, r1, r2);
    cout << "\nDistance while attraction = " << minDis(D, r1, r2);
    return 0;
}


Java
// Java program for max
// and min distance
import java.io.*;
  
class GFG 
{
      
// Function for finding 
// distance between pivots
static int pivotDis(int x0, int y0, 
                    int x1, int y1)
{
    return (int)Math.sqrt((x1 - x0) * 
                          (x1 - x0) + 
                          (y1 - y0) * 
                          (y1 - y0));
}
  
// Function for
// minimum distance
static int minDis(int D, int r1, int r2)
{
    return Math.max((D - r1 - r2), 0);
}
  
// Function for
// maximum distance
static int maxDis(int D, int r1, int r2)
{
    return D + r1 + r2;
}
  
// Driver Code
public static void main (String[] args) 
{
int x0 = 0, y0 = 0, x1 = 8, 
    y1 = 0, r1 = 4, r2 = 5;
int D = pivotDis(x0, y0, x1, y1);
System.out.print( "Distance while " + 
                     "repulsion = " + 
                  maxDis(D, r1, r2));
System.out.print("\nDistance while " + 
                     "attraction = " + 
                   minDis(D, r1, r2));
}
}
  
// This code is contributed by anuj_67.


Python3
# Python 3 program for max and min 
# distance
import math
  
# Function for finding distance between
# pivots
def pivotDis(x0, y0, x1, y1):
  
    return math.sqrt((x1 - x0) * (x1 - x0)
                  + (y1 - y0) * (y1 - y0))
  
# Function for minimum distance
def minDis( D, r1, r2):
  
    return max((D - r1 - r2), 0)
  
# Function for maximum distance
def maxDis( D, r1, r2):
  
    return D + r1 + r2
  
# Drivers code
x0 = 0
y0 = 0
x1 = 8
y1 = 0
r1 = 4
r2 = 5
D = pivotDis(x0, y0, x1, y1)
print("Distance while repulsion = ",
               int(maxDis(D, r1, r2)))
                 
print("Distance while attraction = ",
                    minDis(D, r1, r2))
  
# This code is contributed by Smitha


C#
// C# program for max and min distance
using System;
  
class GFG {
      
    // Function for finding 
    // distance between pivots
    static int pivotDis(int x0, int y0, 
                        int x1, int y1)
    {
        return (int)Math.Sqrt((x1 - x0) * 
                              (x1 - x0) + 
                              (y1 - y0) * 
                              (y1 - y0));
    }
      
    // Function for
    // minimum distance
    static int minDis(int D, int r1, int r2)
    {
        return Math.Max((D - r1 - r2), 0);
    }
      
    // Function for
    // maximum distance
    static int maxDis(int D, int r1, int r2)
    {
        return D + r1 + r2;
    }
      
    // Driver Code
    public static void Main () 
    {
    int x0 = 0, y0 = 0, x1 = 8, 
        y1 = 0, r1 = 4, r2 = 5;
    int D = pivotDis(x0, y0, x1, y1);
      
    Console.WriteLine( "Distance while " + 
                          "repulsion = " + 
                       maxDis(D, r1, r2));
    Console.WriteLine("Distance while " + 
                        "attraction = " + 
                      minDis(D, r1, r2));
    }
}
  
// This code is contributed by anuj_67.


PHP


输出:
Distance while repulsion = 17
Distance while attraction = 0