📜  船上下游速度

📅  最后修改于: 2022-05-13 01:57:59.152000             🧑  作者: Mango

船上下游速度

编写程序求船的上下游速度。为了计算船的上游和下游的速度,我们应该知道船在静水中的速度( B )和水流的速度( S

例子:

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 

方法:沿溪流的方向称为下游,与溪流相反的方向称为上游。因此,在下游的情况下,速度将被添加,而在上游的情况下,速度将被减去。
船顺流速度等于静水中船速与溪流速度之和。
船上游的速度等于船在静水中的速度与溪流的速度之差。因此:
下行速度 = B + S 公里/小时
上游速度 = B – S 公里/小时

C++
// CPP program to find upstream and
// downstream speeds of a boat.
#include 
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


输出:

Speed Downstream = 14 km/hr 
Speed Upstream = 6 km/hr