- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find speed of man from speed of stream and ratio of time with up and down streams in C++
In this problem, we are given two values S and N denoting the speed of stream in Km/h and ratio of time with up and down streams. Our task is to Find speed of man from the speed of stream and ratio of time with up and down streams.
Let’s take an example to understand the problem,
Input
S = 5, N = 2
Output
15
Solution Approach
A simple solution to the problem is by using the mathematical formula for the rowing problems. So, let’s see how the formula will work −
speed of man = x km/h speed of stream = S km/h speed of man downstream i.e. with stream = (x+S) km/h speed of man upstream i.e. against stream = (x-S) km/h Time to travel the distance downstream = T Time to travel the distance upstream = n*T Distance travelled upstream = (x - S)*n*T Distance travelled upstream = (x + S)*T As both the distances are same, (x + S) * T = (x - S)*n*T x + S = nx - nS s + nS = nx - x s*(n + 1) = x(n - 1)
$$x=\frac{S*(S+1)}{(S-1)}$$
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; float calcManSpeed(float S, int n) { return ( S * (n + 1) / (n - 1) ); } int main() { float S = 12; int n = 3; cout<<"The speed of man is "<<calcManSpeed(S, n)<<" km/hr"; return 0; }
Output
The speed of man is 24 km/hr
Advertisements