📌  相关文章
📜  反向移动的两个物体之间的距离等于X的时间

📅  最后修改于: 2021-04-29 01:05:05             🧑  作者: Mango

考虑两个人以相反的方向移动,速度分别为U米/秒V米/秒。任务是找到使它们之间的距离达到X米所需的时间。

例子:

方法:可以使用距离=速度*时间来解决。此处,距离将等于给定范围,即距离= X ,速度将是两个速度的总和,因为它们是沿相反的方向运动,即速度= U + V。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the time for which
// the two policemen can communicate
double getTime(int u, int v, int x)
{
    double speed = u + v;
  
    // time = distance / speed
    double time = x / speed;
    return time;
}
  
// Driver code
int main()
{
    double u = 3, v = 3, x = 3;
    cout << getTime(u, v, x);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
  
    // Function to return the time for which
    // the two policemen can communicate
    static double getTime(int u, int v, int x) 
    {
        double speed = u + v;
  
        // time = distance / speed
        double time = x / speed;
        return time;
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        int u = 3, v = 3, x = 3;
        System.out.println(getTime(u, v, x));
    }
}
  
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 implementation of the approach 
  
# Function to return the time 
# for which the two policemen 
# can communicate 
def getTime(u, v, x): 
  
    speed = u + v 
  
    # time = distance / speed 
    time = x / speed 
    return time 
  
# Driver code 
if __name__ == "__main__":
  
    u, v, x = 3, 3, 3
    print(getTime(u, v, x)) 
  
# This code is contributed
# by Rituraj Jain


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
    // Function to return the time for which
    // the two policemen can communicate
    static double getTime(int u, int v, int x) 
    {
        double speed = u + v;
  
        // time = distance / speed
        double time = x / speed;
        return time;
    }
  
    // Driver code
    public static void Main() 
    {
        int u = 3, v = 3, x = 3;
        Console.WriteLine(getTime(u, v, x));
    }
}
  
// This code is contributed
// by Akanksha Rai


PHP


输出:
0.5