Escriba un programa para encontrar la velocidad del bote río arriba y río abajo. Para calcular la velocidad del bote río arriba y río abajo, debemos conocer la velocidad del bote en aguas tranquilas ( B ) y la velocidad de la corriente ( S )
Ejemplos:
Input : B = 10, S = 4 Output : Speed Downstream = 14 km/hr Speed Upstream = 6 km/hr Input : B = 12, S = 5 Output : Speed Downstream = 17 km/hr Speed Upstream = 7 km/hr
Aproximación: La dirección a lo largo de la corriente se llama río abajo y la dirección opuesta a la corriente se llama río arriba. Entonces, en el caso de aguas abajo, se sumarán las velocidades, mientras que en el caso de aguas arriba, se restarán las velocidades.
La velocidad del bote río abajo es igual a la suma de la velocidad del bote en aguas tranquilas y la velocidad de la corriente.
La velocidad del bote río arriba es igual a la diferencia de la velocidad del bote en aguas tranquilas y la velocidad de la corriente. Por lo tanto:
Velocidad río abajo = B + S km/h
Velocidad río arriba = B – S km/h
C++
// CPP program to find upstream and // downstream speeds of a boat. #include <iostream> using namespace std; // Function to calculate the // speed of boat downstream int Downstream(int b, int s) { return (b + s); } // Function to calculate the // speed of boat upstream int Upstream(int b, int s) { return (b - s); } // Driver function int main() { // Speed of the boat in still water(B) // and speed of the stream (S) in km/hr int B = 10, S = 4; cout << "Speed Downstream = " << Downstream(B, S) << " km/hr\n"<< "Speed Upstream = " << Upstream(B, S) << " km/hr"; return 0; }
Java
// Java program to find upstream and // downstream speeds of a boat. import java.io.*; class GFG { // Function to calculate the // speed of boat downstream static int Downstream(int b, int s) { return (b + s); } // Function to calculate the // speed of boat upstream static int Upstream(int b, int s) { return (b - s); } // Driver function public static void main (String[] args) { // Speed of the boat in still water(B) // and speed of the stream (S) in km/hr int B = 10, S = 4; System.out.println ("Speed Downstream = " + Downstream(B, S) + " km/hr\n"+ "Speed Upstream = " + Upstream(B, S) + " km/hr"); } } // This code is contributed by vt_m.
Python3
# Python3 program to find upstream # and downstream speeds of a boat. # Function to calculate the #speed of boat downstream def Downstream(b, s): return (b + s) # Function to calculate the # speed of boat upstream def Upstream(b, s): return (b - s) # Driver Code # Speed of the boat in still water(B) # and speed of the stream (S) in km/hr B = 10; S = 4 print("Speed Downstream = ", Downstream(B, S), " km/hr", "\nSpeed Upstream = ", Upstream(B, S), " km/hr") # This code is contributed by Anant Agarwal.
C#
// C# program to find upstream and // downstream speeds of a boat. using System; class GFG { // Function to calculate the // speed of boat downstream static int Downstream(int b, int s) { return (b + s); } // Function to calculate the // speed of boat upstream static int Upstream(int b, int s) { return (b - s); } // Driver function public static void Main() { // Speed of the boat in still water(B) // and speed of the stream (S) in km/hr int B = 10, S = 4; Console.WriteLine("Speed Downstream = " + Downstream(B, S) + " km/hr\n" + "Speed Upstream = " + Upstream(B, S) + " km/hr"); } } // This code is contributed by vt_m.
Javascript
<script> // Javascript program to find upstream and // downstream speeds of a boat. // Function to calculate the // speed of boat downstream function Downstream(b, s) { return (b + s); } // Function to calculate the // speed of boat upstream function Upstream(b, s) { return (b - s); } // Driver Code // Speed of the boat in still water(B) // and speed of the stream (S) in km/hr var B = 10; var S = 4; document.write("Speed Downstream = " + Downstream(B, S) +" km/hr" + "<br>" + "Speed Upstream = " + Upstream(B, S) + " km/hr"); // This code is contributed by bunnyram19 </script>
Producción:
Speed Downstream = 14 km/hr Speed Upstream = 6 km/hr
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)