Distancia entre dos puntos recorrida por un barco

Escriba un programa para determinar la distancia (D) entre dos puntos recorridos por un bote, dada la velocidad del bote en aguas tranquilas (B), la velocidad de la corriente (S), el tiempo que tarda en remar un lugar y regresar, es decir t

Ejemplos: 

Input : B = 5, S = 1, T = 1
Output : D = 2.4

Input : B = 5, S = 2, T = 1 
Output : D = 2.1  

La fórmula para la distancia es D = T*(B^2 – S^2)/ (2*B) 

¿Cómo funciona esta fórmula? 

Time taken when person goes upstream, 
   t1 = D/(B-S)
Time taken when person goes downstream, 
  t2 = D/(B+S)

We are given t1 + t2 = T
So, D/(B-S) + D/(B+S) = T
   D = T*(B^2 - S^2)/ (2*B) 

C++

// CPP program to find distance between
// two points using boat speed, stream
// speed and total time.
#include <iostream>
using namespace std;
 
// Function to calculate the distance
// between two points via boat
float distance(float T, float B, float S)
{
    return (T * (((B * B) - (S * S)) / (2 * B)));
}
 
int main()
{
    // Time taken time taken to row a place
    // and come back in hr
    float T = 1;
 
    // Speed of boat in still water in km/hr
    float B = 5;
 
    // Speed of stream in km/hr
    float S = 1;
    cout << "The distance between two points via boat = "
         << distance(T, B, S) << " km";
    return 0;
}

Java

// java program to find distance between
// two points using boat speed, stream
// speed and total time.
import java.io.*;
 
class GFG {
 
    // Function to calculate the distance
    // between two points via boat
    static float distance(float T, float B, float S)
    {
        return (T * (((B * B) - (S * S)) / (2 * B)));
    }
     
    // Driver code
    public static void main (String[] args)
    {
        // Time taken time taken to row a place
        // and come back in hr
        float T = 1;
     
        // Speed of boat in still water in km/hr
        float B = 5;
     
        // Speed of stream in km/hr
        float S = 1;
        System.out.println("The distance between two points via boat = "
                           + distance(T, B, S) + " km");
         
    }
}
 
// This code is contributed by vt_m.

Python3

# Python 3 program to find distance
# between two points using boat speed,
# stream speed and total time.
 
# Function to calculate the distance
# between two points via boat
def distance( T, B, S):
 
    return (T * (((B * B) - (S * S)) / (2 * B)))
 
# Driver Code
if __name__ == "__main__":
     
    # Time taken time taken to row
    # a place and come back in hr
    T = 1
 
    # Speed of boat in still
    # water in km/hr
    B = 5
 
    # Speed of stream in km/hr
    S = 1
    print("The distance between two "+
          "points via boat =",
           distance(T, B, S), "km")
 
# This code is contributed
# by ChitraNayal

C#

// C# program to find distance between
// two points using boat speed, stream
// speed and total time.
using System;
 
class GFG {
 
    // Function to calculate the distance
    // between two points via boat
    static float distance(float T, float B, float S)
    {
        return (T * (((B * B) - (S * S)) / (2 * B)));
    }
 
    // Driver code
    public static void Main()
    {
        // Time taken time taken to row a place
        // and come back in hr
        float T = 1;
 
        // Speed of boat in still water in km/hr
        float B = 5;
 
        // Speed of stream in km/hr
        float S = 1;
        Console.WriteLine("The distance between two points via boat = " +
                                              distance(T, B, S) + " km");
    }
}
 
// This code is contributed by vt_m.

Javascript

<script>
 
// Javascript program to find distance
// between two points using boat speed,
// stream speed and total time.
   
// Function to calculate the speed
// of boat in still water
function  distance(T,B,S)
{
    return (T * (((B * B) - (S * S)) / (2 * B)));
}
   
// Driver Code
var T = 1;
var B = 5;
var S = 1;
 
document.write("The distance between " +
               "two points via boat = " +
               distance(T, B, S) + " km ");
                
// This code is contributed by bunnyram19
 
</script>

Producción: 

The distance between two points via boat = 2.4 km

Complejidad de tiempo: O(1)

Espacio Auxiliar: O(1)
 

Publicación traducida automáticamente

Artículo escrito por mohitw16 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *