📜  程序根据声音速度查找火车速度

📅  最后修改于: 2021-04-26 10:22:04             🧑  作者: Mango

每隔X分钟从同一地点发射两把枪。乘火车接近射击地点的人会在“ Y”分钟的间隔内听到射击声音。任务是找到“ S”,即火车的速度。
注意:空气中的声音速度为固定值330m / s。

例子:

方法:以第一个示例为例,

  • 火车经过的距离,以Y分钟为单位
    =声音所覆盖的距离,以(X – Y)分钟为单位
    = 330 *(X – Y)* 60分钟(距离=音速*时间)
  • 火车速度
    =(330 *(X – Y)* 60)/ Y(速度=距离/时间)
    =(330 * 60 *(X – Y))/ 1000 *(60 / y)
    = 1188 [(X – Y)/ Y] km / hr
  • 现在,根据第一个例子
    时差,即(X – Y)= 14.00 – 13.30 = 30秒
    火车速度= 1188 * [(14 – 13.5)/ 13.5] = 44公里/小时
  • 可以使用以下公式计算得出:

下面是上述方法的实现。

C++
// C++ implementation of the approach
 
#include 
using namespace std;
 
// Function to find the
// Speed of train
float speedOfTrain(float X, float Y)
{
 
    float Speed = 0;
 
    Speed = 1188 * ((X - Y) / Y);
 
    return Speed;
}
 
// Driver code
int main()
{
    float X = 8, Y = 7.2;
 
    // calling Function
    cout << speedOfTrain(X, Y)
         << " km/hr";
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
    // Function to find the
    // Speed of train
    static int speedOfTrain(float X, float Y)
    {
        float Speed;
 
        Speed = 1188 * ((X - Y) / Y);
 
        return (int)Speed;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        float X = 8f, Y = 7.2f;
 
        // calling Function
        int result = (speedOfTrain(X, Y));
        System.out.println(result + " km/hr");
    }
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach
from math import ceil
 
# Function to find the
# Speed of train
def speedOfTrain(X, Y):
    Speed = 0
 
    Speed = 1188 * ((X - Y) / Y)
 
    return Speed
 
# Driver code
if __name__ == '__main__':
    X = 8
    Y = 7.2
 
    # calling Function
    print(ceil(speedOfTrain(X, Y)),
                   end = " km/hr")
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to find the
    // Speed of train
    static int speedOfTrain(float X, float Y)
    {
        float Speed;
 
        Speed = 1188 * ((X - Y) / Y);
 
        return (int)Speed;
    }
 
    // Driver code
    public static void Main()
    {
        float X = 8f, Y = 7.2f;
 
        // calling Function
        int result = (speedOfTrain(X, Y));
        Console.Write(result + " km/hr");
    }
}
 
// This code is contributed by anuj_67..


Javascript


输出:
132 km/hr