从流的速度和时间与上游和下游的比率中找到人的速度
给定以公里/小时为单位的水流速度,程序以公里/小时为单位计算人的速度。假设这个人在河上游划船的时间是在河下游划船的时间的 n 倍(其中 n > 1)。
例子:
Input : 5.5 4
Output : speed of man is 9.16667 km/hr
Input : 4 3
Output : speed of man is 8 km/hr
使用的方法:
设人在静水中的速度 = x km/hr
设水流速度 = y km/hr
然后,下游速度 = (x + y) km/hr
上游速度 = (x – y) 公里/小时
让下游旅行所需的时间 = t hr
然后,逆流而上的时间 = nt hr
下游行驶距离 = (x + y)t km ————-(1)
逆行距离 = (x – y)tn km ————-(2)
从(1)和(2)
(x + y)t = (x – y)tn
给出: (x + y) = (x – y)n
给出:x + y = nx – ny
给出:nx – x = ny + y
这给出: x(n – 1) = y(n + 1)
给出: x = y(n + 1)/(n – 1)
C++
// C++ implementation
#include
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
Javascript
输出:
speed of man is 3.22 km/hr