Dada la longitud y la velocidad del tren y el tiempo que tarda el tren en pasar el puente o el túnel, la tarea es encontrar la longitud del puente.
Ejemplos:
Entrada : longitud del tren = 120 metros, Velocidad = 30 m/seg, tiempo = 18 s
Salida : longitud del puente = 420 metros.
Entrada : longitud del tren = 130 metros, Velocidad = 25 m/seg, tiempo = 21 s
Salida : longitud del puente = 395 metros.
Aproximación:
Sea la longitud del puente .
Como se sabe que Velocidad = Distancia / Tiempo
Por lo tanto:
Distancia total = (longitud del tren + longitud del puente)
=> Velocidad del tren = (longitud del tren + x) / Tiempo.
=> x = (Velocidad del tren * tiempo) – Longitud del tren.
Fórmula:
longitud del puente = (velocidad del tren * tiempo necesario para cruzar el puente) – longitud del tren.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program to implement above code. #include <bits/stdc++.h> 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
<?php // PHP Program to implement // above code // function to calculate // the length of bridge function bridge_length($trainLength, $Speed, $Time) { return (($Time * $Speed) - $trainLength); } // Driver Code // Assuming the input variables $trainLength = 120; $Speed = 30; $Time = 18; echo "Length of bridge = ". bridge_length($trainLength, $Speed, $Time). " meters"; // This code is contributed by Raj ?>
Javascript
<script> // Javascript program to implement above code. // Function to calculate the length of bridge. function bridge_length(trainLength, Speed, Time) { return ((Time * Speed) - trainLength); } // Driver code // Assuming the input variables var trainLength = 120; var Speed = 30; var Time = 18; document.write("Length of bridge = " + bridge_length(trainLength, Speed,Time) + " meters"); // This code is contributed by Khushboogoyal499 </script>
Length of bridge = 420 meters
Complejidad Temporal: O(1)
Espacio Auxiliar: O(1), ya que no se ha tomado ningún espacio extra.
Publicación traducida automáticamente
Artículo escrito por Naman_Garg y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA