📜  检查两个从不同角度出发的人是否见过面

📅  最后修改于: 2021-04-26 11:04:29             🧑  作者: Mango

有两个人从两个不同的位置开始,比如说x1和x2。两者都可以分别向前跳v1和v2米。考虑到双方跳的次数必须相同,我们必须找到两者是否会碰面。

如果他们见面,请打印“是”,
打印“不”,他们不会。

例子 :

Input  : x1 = 5, v1 = 8, x2 = 4, v2 = 7
Output : No
Explanation: The first person is starting ahead of the second one.
and his speed is also greater than the second one, so they will never meet.

Input  : x1 = 6, v1 = 6, x2 = 4, v2 = 8
Output : Yes

天真的方法:在这种情况下,我们计算每个人在每次跳跃后的位置,并检查他们是否落在同一地点。这个具有复杂度O(n)。

C++
// C++ program to find if two people
// starting from different positions
// ever meet or not.
#include 
using namespace std;
  
bool everMeet(int x1, int x2, int v1, int v2)
{
    // If speed of a person at a position before
    // other person is smaller, then return false.
    if (x1 < x2 && v1 <= v2)
       return false;
    if (x1 > x2 && v1 >=v2)
       return false;  
    
    // Making sure that x1 is greater
    if (x1 < x2) 
    { 
        swap(x1, x2); 
        swap(v1, v2); 
    }     
    
     // Until one person crosses other 
     while (x1 >= x2) { 
        if (x1 == x2) 
            return true;
          
        // first person taking one
        // jump in each iteration
        x1 = x1 + v1; 
          
        // second person taking
        // one jump in each iteration
        x2 = x2 + v2; 
    }
  
    return false;   
}
  
// Driver code
int main()
{
    int x1 = 5, v1 = 8, x2 = 4, v2 = 7;
    if (everMeet(x1, x2, v1, v2))
        printf("Yes");    
    else 
        printf("No");
    return 0;
}


Java
// Java program to find 
// if two people starting 
// from different positions
// ever meet or not.
import java.io.*;
  
class GFG
{
    static void swap(int a, int b)
    {
        int t = a;
        a = b;
        b = t;
    }
    static boolean everMeet(int x1, int x2,
                            int v1, int v2)
    {
        // If speed of a person 
        // at a position before 
        // other person is smaller, 
        // then return false.
        if (x1 < x2 && v1 <= v2)
        return false;
        if (x1 > x2 && v1 >= v2)
        return false; 
          
        // Making sure that 
        // x1 is greater
        if (x1 < x2)
        {
            swap(x1, x2);
            swap(v1, v2);
        } 
      
        // Until one person 
        // crosses other
        while (x1 >= x2) 
        {
            if (x1 == x2) 
                return true;
              
            // first person taking one
            // jump in each iteration
            x1 = x1 + v1; 
              
            // second person taking
            // one jump in each iteration
            x2 = x2 + v2; 
        }
      
        return false; 
    }
      
    // Driver code
    public static void main (String[] args) 
    {
        int x1 = 5, v1 = 8, 
            x2 = 4, v2 = 7;
        if (everMeet(x1, x2, v1, v2))
            System.out.println("Yes"); 
        else
            System.out.println("No");
    }
}
  
// This code is contributed 
// by akt_mit


Python3
# Python3 program to find if two 
# people starting from different 
# positions ever meet or not.
  
def everMeet(x1, x2, v1, v2):
  
    # If speed of a person at 
    # a position before other
    # person is smaller, then 
    # return false.
    if (x1 < x2 and v1 <= v2):
        return False;
    if (x1 > x2 and v1 >= v2):
        return False; 
  
    # Making sure that
    # x1 is greater
    if (x1 < x2):
  
        x1, x2 = x2,x1;
        v1, v2 = v2,v1;
      
  
    # Until one person
    # crosses other
    while (x1 >= x2): 
      
        if (x1 == x2): 
            return True;
          
        # first person taking one
        # jump in each iteration
        x1 = x1 + v1; 
          
        # second person taking
        # one jump in each iteration
        x2 = x2 + v2; 
      
  
    return False; 
  
  
# Driver code
x1 = 5;
v1 = 8;
x2 = 4;
v2 = 7;
if (everMeet(x1, x2,v1, v2)):
    print("Yes"); 
else:
    print("No");
  
# This code is contributed by mits


C#
// C# program to find if two 
// people starting from different 
// positions ever meet or not.
using System;
  
class GFG
{
    static void swap(ref int a, 
                     ref int b)
    {
        int t = a;
        a = b;
        b = t;
    }
    static bool everMeet(int x1, int x2, 
                         int v1, int v2)
    {
        // If speed of a person at a 
        // position before other person
        // is smaller, then return false.
        if (x1 < x2 && v1 <= v2)
        return false;
        if (x1 > x2 && v1 >= v2)
        return false; 
          
        // Making sure that x1 is greater
        if (x1 < x2)
        {
            swap(ref x1, ref x2);
            swap(ref v1, ref v2);
        } 
      
        // Until one person crosses other
        while (x1 >= x2) 
        {
            if (x1 == x2) 
                return true;
              
            // first person taking one
            // jump in each iteration
            x1 = x1 + v1; 
              
            // second person taking
            // one jump in each iteration
            x2 = x2 + v2; 
        }
      
        return false; 
    }
      
    // Driver code
    static void Main()
    {
        int x1 = 5, v1 = 8, 
            x2 = 4, v2 = 7;
        if (everMeet(x1, x2, v1, v2))
            Console.Write("Yes"); 
        else
            Console.Write("No");
    }
}
  
// This code is contributed by 
// Manish Shaw(manishshaw1)


PHP
 $x2 && $v1 >= $v2)
        return false; 
  
    // Making sure that
    // x1 is greater
    if ($x1 < $x2)
    {
        list($x1, $x2) = array($x2, 
                               $x1);
        list($v1, $v2) = array($v2, 
                               $v1);
      
    } 
  
    // Until one person
    // crosses other
    while ($x1 >= $x2) 
    {
        if ($x1 == $x2) 
            return true;
          
        // first person taking one
        // jump in each iteration
        $x1 = $x1 + $v1; 
          
        // second person taking
        // one jump in each iteration
        $x2 = $x2 + $v2; 
    }
  
    return false; 
}
  
// Driver code
$x1 = 5;
$v1 = 8;
$x2 = 4;
$v2 = 7;
if (everMeet($x1, $x2,
             $v1, $v2))
    echo "Yes"; 
else
    echo "No";
  
// This code is contributed by ajit
?>


C++
// C++ program to find if two people
// starting from different positions
// ever meet or not.
#include 
using namespace std;
  
bool everMeet(int x1, int x2, int v1, int v2)
{
  
    // If speed of a person at a position before
    // other person is smaller, then return false.
    if (x1 < x2 && v1 <= v2)
       return false;
    if (x1 > x2 && v1 >= v2)
       return false;  
  
    // Making sure that x1 is greater
    if (x1 < x2)
    {
        swap(x1, x2);
        swap(v1, v2);
    }    
  
    // checking if relative speed is
    // a factor of relative distance or not
    return ((x1 - x2) % (v1 - v2) == 0); 
}
  
// Driver code
int main()
{
    int x1 = 5, v1 = 8, x2 = 4, v2 = 7;
    if (everMeet(x1, x2, v1, v2))
        printf("Yes");    
    else 
        printf("No");
    return 0;
}


Java
// Java program to find if two people 
// starting from different positions 
// ever meet or not. 
  
public class GFG {
  
    static boolean everMeet(int x1, int x2, int v1, int v2) {
  
        // If speed of a person at a position before 
        // other person is smaller, then return false. 
        if (x1 < x2 && v1 <= v2) {
            return false;
        }
        if (x1 > x2 && v1 >= v2) {
            return false;
        }
  
        // Making sure that x1 is greater 
        if (x1 < x2) {
            swap(x1, x2);
            swap(v1, v2);
        }
  
        // checking if relative speed is 
        // a factor of relative distance or not 
        return ((x1 - x2) % (v1 - v2) == 0);
    }
  
    static void swap(int a, int b) {
        int t = a;
        a = b;
        b = t;
    }
  
    public static void main(String[] args) {
        int x1 = 5, v1 = 8, x2 = 4, v2 = 7;
        if (everMeet(x1, x2, v1, v2)) {
            System.out.printf("Yes");
        } else {
            System.out.printf("No");
        }
    }
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find if two people
# starting from different positions
# ever meet or not.
  
def everMeet(x1, x2, v1, v2):
  
    # If speed of a person at a position before
    # other person is smaller, then return false.
    if (x1 < x2 and v1 <= v2):
        return False;
        if (x1 > x2 and v1 >= v2):
            return False; 
              
            # Making sure that x1 is greater
            if (x1 < x2):
                swap(x1, x2)
                swap(v1, v2)
                  
                # checking if relative speed is
                # a factor of relative distance or not
                return ((x1 - x2) % (v1 - v2) == 0) 
                def swap(a, b):
                    t = a
                    a = b
                    b = t
# Driver code
def main():
      
    x1, v1, x2, v2 =5, 8, 4, 7
    if (everMeet(x1, x2, v1, v2)):
        print("Yes") 
    else:
        print("No")
  
if __name__ == '__main__':
    main()
      
      
# This code is contributed by 29AjayKumar


C#
//C# program to find if two people 
// starting from different positions 
// ever meet or not. 
using System; 
public class GFG{
  
  
    static bool everMeet(int x1, int x2, int v1, int v2) { 
  
        // If speed of a person at a position before 
        // other person is smaller, then return false. 
        if (x1 < x2 && v1 <= v2) { 
            return false; 
        } 
        if (x1 > x2 && v1 >= v2) { 
            return false; 
        } 
  
        // Making sure that x1 is greater 
        if (x1 < x2) { 
            swap(x1, x2); 
            swap(v1, v2); 
        } 
  
        // checking if relative speed is 
        // a factor of relative distance or not 
        return ((x1 - x2) % (v1 - v2) == 0); 
    } 
  
    static void swap(int a, int b) { 
        int t = a; 
        a = b; 
        b = t; 
    } 
  
    public static void Main() { 
        int x1 = 5, v1 = 8, x2 = 4, v2 = 7; 
        if (everMeet(x1, x2, v1, v2)) { 
            Console.WriteLine("Yes"); 
        } else { 
            Console.WriteLine("No"); 
        } 
    } 
} 
  
  
// This code is contributed by 29AjayKumar


PHP
 $x2 && $v1 >= $v2)
        return false; 
  
    // Making sure that x1 is greater
    if ($x1 < $x2)
    {
        list($x1, $x2) = array($x2, $x1);
        list($v2, $v1) = array($v1, $v2);
    } 
  
    // checking if relative speed is
    // a factor of relative distance or not
    return (($x1 - $x2) % ($v1 - $v2) == 0); 
}
  
// Driver code
$x1 = 5; $v1 = 8; 
$x2 = 4; $v2 = 7;
if (everMeet($x1, $x2, $v1, $v2))
    print("Yes"); 
else
    print("No");
  
// This code is contributed by mits
?>


输出:
No

高效方法:另一种方法是使用相对速度的概念。如果X1X2的距离为D ,则X1的相对速度应为D的因数,以便两者都落在同一点上。

例子:
X1 = 10
X2 = 25
V1 = 10
V2 = 8
由于此处D = 15相对速度= 2(这不是D的因数),因此它们将永远不会满足。

该算法的时间复杂度为O(1)
实施如下:

C++

// C++ program to find if two people
// starting from different positions
// ever meet or not.
#include 
using namespace std;
  
bool everMeet(int x1, int x2, int v1, int v2)
{
  
    // If speed of a person at a position before
    // other person is smaller, then return false.
    if (x1 < x2 && v1 <= v2)
       return false;
    if (x1 > x2 && v1 >= v2)
       return false;  
  
    // Making sure that x1 is greater
    if (x1 < x2)
    {
        swap(x1, x2);
        swap(v1, v2);
    }    
  
    // checking if relative speed is
    // a factor of relative distance or not
    return ((x1 - x2) % (v1 - v2) == 0); 
}
  
// Driver code
int main()
{
    int x1 = 5, v1 = 8, x2 = 4, v2 = 7;
    if (everMeet(x1, x2, v1, v2))
        printf("Yes");    
    else 
        printf("No");
    return 0;
}

Java

// Java program to find if two people 
// starting from different positions 
// ever meet or not. 
  
public class GFG {
  
    static boolean everMeet(int x1, int x2, int v1, int v2) {
  
        // If speed of a person at a position before 
        // other person is smaller, then return false. 
        if (x1 < x2 && v1 <= v2) {
            return false;
        }
        if (x1 > x2 && v1 >= v2) {
            return false;
        }
  
        // Making sure that x1 is greater 
        if (x1 < x2) {
            swap(x1, x2);
            swap(v1, v2);
        }
  
        // checking if relative speed is 
        // a factor of relative distance or not 
        return ((x1 - x2) % (v1 - v2) == 0);
    }
  
    static void swap(int a, int b) {
        int t = a;
        a = b;
        b = t;
    }
  
    public static void main(String[] args) {
        int x1 = 5, v1 = 8, x2 = 4, v2 = 7;
        if (everMeet(x1, x2, v1, v2)) {
            System.out.printf("Yes");
        } else {
            System.out.printf("No");
        }
    }
}
  
// This code is contributed by 29AjayKumar 

Python3

# Python3 program to find if two people
# starting from different positions
# ever meet or not.
  
def everMeet(x1, x2, v1, v2):
  
    # If speed of a person at a position before
    # other person is smaller, then return false.
    if (x1 < x2 and v1 <= v2):
        return False;
        if (x1 > x2 and v1 >= v2):
            return False; 
              
            # Making sure that x1 is greater
            if (x1 < x2):
                swap(x1, x2)
                swap(v1, v2)
                  
                # checking if relative speed is
                # a factor of relative distance or not
                return ((x1 - x2) % (v1 - v2) == 0) 
                def swap(a, b):
                    t = a
                    a = b
                    b = t
# Driver code
def main():
      
    x1, v1, x2, v2 =5, 8, 4, 7
    if (everMeet(x1, x2, v1, v2)):
        print("Yes") 
    else:
        print("No")
  
if __name__ == '__main__':
    main()
      
      
# This code is contributed by 29AjayKumar

C#

//C# program to find if two people 
// starting from different positions 
// ever meet or not. 
using System; 
public class GFG{
  
  
    static bool everMeet(int x1, int x2, int v1, int v2) { 
  
        // If speed of a person at a position before 
        // other person is smaller, then return false. 
        if (x1 < x2 && v1 <= v2) { 
            return false; 
        } 
        if (x1 > x2 && v1 >= v2) { 
            return false; 
        } 
  
        // Making sure that x1 is greater 
        if (x1 < x2) { 
            swap(x1, x2); 
            swap(v1, v2); 
        } 
  
        // checking if relative speed is 
        // a factor of relative distance or not 
        return ((x1 - x2) % (v1 - v2) == 0); 
    } 
  
    static void swap(int a, int b) { 
        int t = a; 
        a = b; 
        b = t; 
    } 
  
    public static void Main() { 
        int x1 = 5, v1 = 8, x2 = 4, v2 = 7; 
        if (everMeet(x1, x2, v1, v2)) { 
            Console.WriteLine("Yes"); 
        } else { 
            Console.WriteLine("No"); 
        } 
    } 
} 
  
  
// This code is contributed by 29AjayKumar

的PHP

 $x2 && $v1 >= $v2)
        return false; 
  
    // Making sure that x1 is greater
    if ($x1 < $x2)
    {
        list($x1, $x2) = array($x2, $x1);
        list($v2, $v1) = array($v1, $v2);
    } 
  
    // checking if relative speed is
    // a factor of relative distance or not
    return (($x1 - $x2) % ($v1 - $v2) == 0); 
}
  
// Driver code
$x1 = 5; $v1 = 8; 
$x2 = 4; $v2 = 7;
if (everMeet($x1, $x2, $v1, $v2))
    print("Yes"); 
else
    print("No");
  
// This code is contributed by mits
?>
输出 :
No