📜  程序使用速度和火车长度来查找桥的长度

📅  最后修改于: 2021-06-01 02:26:21             🧑  作者: Mango

给定火车的长度和速度,以及火车通过桥梁或隧道所花费的时间,任务是找到桥梁的长度。
例子:

方法:
设桥的长度为x
众所周知,速度=距离/时间
所以:

公式:

下面是上述方法的实现:

C++
// C++ Program to implement above code.
 
#include 
using namespace std;
 
// function to calculate the length of bridge.
int bridge_length(int trainLength, int Speed, int Time)
{
    return ((Time * Speed) - trainLength);
}
 
// Driver Code
int main()
{
    // Assuming the input variables
    int trainLength = 120;
    int Speed = 30;
    int Time = 18;
 
    cout << "Length of bridge = "
         << bridge_length(trainLength, Speed, Time)
         << " meters";
 
    return 0;
}


Java
//Java Program to implement above code.
 
public class GFG {
     
    //function to calculate the length of bridge.
    static int bridge_length(int trainLength,
                            int Speed, int Time)
    {
    return ((Time * Speed) - trainLength);
    }
 
    //Driver Code
    public static void main(String[] args) {
         
        // Assuming the input variables
        int trainLength = 120;
        int Speed = 30;
        int Time = 18;
 
        System.out.print("Length of bridge = "+
            bridge_length(trainLength, Speed,Time)
            +" meters");
    }
}


Python 3
# Python 3 Program to implement above code.
 
# function to calculate the length of bridge.
def bridge_length(trainLength, Speed, Time) :
 
    return ((Time * Speed) - trainLength)
     
# Driver Code
if __name__ == "__main__" :
 
    # Assuming the input variables
    trainLength = 120
    Speed = 30
    Time = 18
 
    print("Length of bridge = ",bridge_length
            (trainLength, Speed, Time),"meters")
 
# This code is contributed by ANKITRAI1


C#
// C# Program to implement above code
using System;
 
class GFG
{
 
// function to calculate
// the length of bridge
static int bridge_length(int trainLength,
                         int Speed, int Time)
{
    return ((Time * Speed) - trainLength);
}
 
// Driver Code
static void Main()
{
    // Assuming the input variables
    int trainLength = 120;
    int Speed = 30;
    int Time = 18;
 
        Console.Write("Length of bridge = " +
        bridge_length(trainLength, Speed, Time) +
                                      " meters");
}
}
 
// This code is contributed by Raj


PHP


Javascript


输出:
Length of bridge = 420 meters
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”