📜  环形道路上不同的会合点数

📅  最后修改于: 2021-04-24 22:21:30             🧑  作者: Mango

考虑两辆汽车AB ,它们在环形道路上无限地行驶(顺时针或逆时针)。给定汽车ab的速度。如果a或b为正,则指示它们沿顺时针方向移动,否则指示它们沿逆时针方向移动。任务是找到彼此相遇的不同点的数量。

例子:

Input : a = 1, b = -1
Output : 2

Explanation
Car A is moving clockwise while Car B is moving anti-clockwise but their
speeds are same, so they will meet at two points i.e at the starting point
and diametrically corresponding opposite point on the road.

Input : a = 1, b = 2
Output : 1

必备条件:GCD |液晶模组

方法
令圆形道路的周长为d
假设汽车AB花费的时间分别为t at b 。它们的相对速度为a – b

AB从起点开始,一段时间后,它们将再次在起点相遇。可以通过t at bLCM来计算该时间。在此时间段内,他们可能会在某些特定地点见面。观察到,他们在起点见面后便继续在同一地点见面。

在起点再次开会所需的时间是

T1 = LCM(ta, tb) = LCM(d/a, d/b) = d/GCD(a, b)

让他们在时间段T 1中相遇N次。
因此,他们两次连续见面之间的时间延迟为T 2,可以这样计算:

以下是上述方法的实现:

C++
// CPP Program to find number of distinct point of meet on a circular road
#include 
using namespace std;
  
// Returns the GCD of two number.
int gcd(int a, int b)
{
    int c = a % b;
    while (c != 0) {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}
  
// Returns the number of distinct meeting points.
int numberOfmeet(int a, int b)
{
    int ans;
  
    // Find the relative speed.
    if (a > b)
        ans = a - b;
    else
        ans = b - a;
  
    // convert the negative value to positive.
    if (a < 0)
        a = a * (-1);
  
    if (b < 0)
        b = b * (-1);
  
    return ans / gcd(a, b);
}
  
// Driver Code
int main()
{
    int a = 1, b = -1;
  
    cout << numberOfmeet(a, b) << endl;
    return 0;
}


Java
// Java Program to find number 
// of distinct point of meet 
// on a circular road
import java.io.*;
  
class GFG 
{
      
// Returns the GCD 
// of two number.
static int gcd(int a, int b)
{
    int c = a % b;
    while (c != 0) 
    {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}
  
// Returns the number of
// distinct meeting points.
static int numberOfmeet(int a, 
                        int b)
{
    int ans;
  
    // Find the relative speed.
    if (a > b)
        ans = a - b;
    else
        ans = b - a;
  
    // convert the negative 
    // value to positive.
    if (a < 0)
        a = a * (-1);
  
    if (b < 0)
        b = b * (-1);
  
    return ans / gcd(a, b);
}
  
// Driver Code
public static void main (String[] args)
{
    int a = 1, b = -1;
    System.out.println(numberOfmeet(a, b));
}
}
  
// This code is contributed by @ajit


Python3
# Python3 Program to find 
# number of distinct point 
# of meet on a circular road
import math
  
# Returns the number of
# distinct meeting points.
def numberOfmeet(a, b):
    ans = 0;
      
    # Find the relative speed.
    if (a > b):
        ans = a - b;
    else:
        ans = b - a;
          
    # convert the negative
    # value to positive.
    if (a < 0):
        a = a * (-1);
    if (b < 0):
        b = b * (-1);
    return int(ans / math.gcd(a, b));
  
# Driver Code
a = 1;
b = -1;
print(numberOfmeet(a, b));
  
# This code is contributed by mits


C#
// C# Program to find number 
// of distinct point of meet 
// on a circular road
using System;
  
class GFG
{
  
// Returns the GCD 
// of two number.
static int gcd(int a, int b)
{
    int c = a % b;
    while (c != 0) 
    {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}
  
// Returns the number of
// distinct meeting points.
static int numberOfmeet(int a, 
                        int b)
{
    int ans;
  
    // Find the relative speed.
    if (a > b)
        ans = a - b;
    else
        ans = b - a;
  
    // convert the negative 
    // value to positive.
    if (a < 0)
        a = a * (-1);
  
    if (b < 0)
        b = b * (-1);
  
    return ans / gcd(a, b);
}
  
// Driver Code
static public void Main ()
{
    int a = 1, b = -1;
    Console.WriteLine(
            numberOfmeet(a, b));
}
}
  
// This code is contributed 
// by @ajit


PHP
 $b)
        $ans = $a - $b;
    else
        $ans = $b - $a;
  
    // convert the negative
    // value to positive.
    if ($a < 0)
        $a = $a * (-1);
  
    if ($b < 0)
        $b = $b * (-1);
  
    return $ans / gcd($a, $b);
}
  
// Driver Code
$a = 1;
$b = -1;
  
echo numberOfmeet($a, $b)."\n";
      
// This code is contributed by mits
?>


输出:

2