Programa para hallar la velocidad del hombre en km/h, dada la velocidad de la corriente en km/h. Suponga que el hombre tarda n veces (donde n > 1) tanto en remar río arriba como en remar río abajo.
Ejemplos:
Input : 5.5 4 Output : speed of man is 9.16667 km/hr Input : 4 3 Output : speed of man is 8 km/hr
Método utilizado:
Sea la velocidad del hombre en aguas tranquilas = x km/hr
Sea la velocidad de la corriente = y km/hr
Entonces, la velocidad corriente abajo = (x + y) km/hr
la velocidad corriente arriba = (x – y) km/hr
Sea el tiempo necesario para viajar río abajo = t hr
Entonces, el tiempo necesario para viajar río arriba = nt hr
Distancia recorrida río abajo = (x + y)t km ————-(1)
Distancia recorrida río arriba = (x – y)tn km — ———-(2)
de (1) y (2)
(x + y)t = (x – y)tn
que da: (x + y) = (x – y)n
que da: x + y = nx – ny
lo que da: nx – x = ny + y
lo que da: x(n – 1) = y(n + 1)
lo que da: x = y(n + 1)/(n – 1)
C++
// C++ implementation #include <bits/stdc++.h> using namespace std; // function to return speed of man float speed_man(float y1, int n1) { // return the speed of man in km/hr return ( y1 * (n1 + 1) / (n1 - 1) ); } // Driver function int main() { // y is speed of stream in km/hr float y = 2.3; int n = 6; float speed = speed_man(y, n); cout << "speed of man is " << speed << " km/hr" << endl; return 0; }
Java
// Java Implementation import java.lang.*; import java.util.*; public class GeeksforGeeks { // function to return speed of man static float speed_man(float y1, int n1) { // x is speed of man return ( y1 * (n1 + 1) / (n1 - 1) ); } // Driver function public static void main(String[] args) { // y is speed of stream in km/hr float y = 2.3f; int n = 6; float speed = speed_man(y, n); System.out.println("speed of man is " + speed + " km/hr"); } }
Python3
# Python3 implementation to find # the speed of man in km/hr # Function to return speed of man def speed_man(y1, n1): # return the speed of man in km/hr return (y1 * (n1 + 1) / (n1 - 1)) # Driver Code # y is speed of stream in km/hr y = 2.3; n = 6 speed = speed_man(y, n) print("speed of man is ", "%.2f"%speed, " km/hr") # This code is contributed by Anant Agarwal.
C#
// C# Implementation using System; public class GFG { // function to return speed of man static float speed_man(float y1, int n1) { // x is speed of man return ( y1 * (n1 + 1) / (n1 - 1) ); } // Driver function public static void Main() { // y is speed of stream in km/hr float y = 2.3f; int n = 6; float speed = speed_man(y, n); Console.WriteLine("speed of man is " + speed + " km/hr"); } } // This code is contributed by vt_m.
PHP
<?php // PHP implementation to find // the speed of man in km/hr // function to return // speed of man function speed_man($y1, $n1) { // return the speed of // man in km/hr return ($y1 * ($n1 + 1) / ($n1 - 1) ); } // Driver function // y is speed of // stream in km/hr $y = 2.3; $n = 6; $speed = speed_man($y, $n); echo "speed of man is " , $speed , " km/hr" ; // This code is contributed by anuj_67. ?>
Javascript
<script> // javascript Implementation // function to return speed of man function speed_man( y1, n1) { // x is speed of man return ( y1 * (n1 + 1) / (n1 - 1) ); } // Driver function // y is speed of stream in km/hr var y = 2.3; var n = 6; var speed = speed_man(y, n).toFixed(2) document.write("speed of man is " + speed + " km/hr"); </script>
Producción:
speed of man is 3.22 km/hr
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Sahil_Bansall y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA