从水流速度和所用时间看船在静水中的速度
编写一个程序来确定小船在静水中的速度 (B)小时)。
例子:
Input : T1 = 4, T2 = 2, S = 5
Output : B = 15
Input : T1 = 6, T2 = 1, S = 7
Output : B = 9.8
先决条件:上下游船速
可以使用以下公式计算静止水中的船速。
B = S*((T1 + T2) / (T1 – T2))
这个公式是如何工作的?
由于点是相同的,因此在上游行驶的距离应该与下游相同。
所以,
(B – S) * T1 = (B + S) * T2
B(T1 – T2) = S*(T1 + T2)
B = S*(T1 + T2)/(T1 – T2)
C++
// CPP program to find speed of boat in still water
// from speed of stream and times taken in downsteam
// and upstream
#include
using namespace std;
// Function to calculate the speed of boat in still water
float still_water_speed(float S, float T1, float T2)
{
return (S * (T1 + T2) / (T1 - T2));
}
int main()
{
float S = 7, T1 = 6, T2 = 1;
cout << "The speed of boat in still water = "<<
still_water_speed(S, T1, T2)<<" km/ hr ";
return 0;
}
Java
// Java program to find speed of boat in still water
// from speed of stream and times taken in downsteam
// and upstream
import java.io.*;
class GFG
{
// Function to calculate the
// speed of boat in still water
static float still_water_speed(float S, float T1, float T2)
{
return (S * (T1 + T2) / (T1 - T2));
}
// Driver code
public static void main (String[] args) {
float S = 7, T1 = 6, T2 = 1;
System.out.println("The speed of boat in still water = "+
still_water_speed(S, T1, T2)+" km/ hr ");
}
}
// This code is contributed by vt_m.
Python3
# Python3 program to find speed of boat
# in still water from speed of stream and
# times taken in downsteam and upstream
# Function to calculate the
# speed of boat in still water
def still_water_speed(S, T1, T2):
return (S * (T1 + T2) / (T1 - T2))
# Driver code
S = 7; T1 = 6; T2 = 1
print("The speed of boat in still water = ",
still_water_speed(S, T1, T2), " km/ hr ")
# This code is contributed by Anant Agarwal.
C#
// C# program to find speed of boat
// in still water from speed of stream
// and times taken in downsteam
// and upstream
using System;
class GFG {
// Function to calculate the
// speed of boat in still water
static float still_water_speed(float S, float T1,
float T2)
{
return (S * (T1 + T2) / (T1 - T2));
}
// Driver code
public static void Main()
{
float S = 7, T1 = 6, T2 = 1;
Console.WriteLine("The speed of boat in still water = " +
still_water_speed(S, T1, T2) + " km/ hr ");
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出:
The speed of boat in still water = 9.8 km/hr