Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

C++에서 스트림의 속도와 상하 스트림의 시간 비율에서 사람의 속도 찾기

<시간/>

이 문제에서 스트림의 속도(Km/h)를 나타내는 두 개의 값 S와 N이 주어지고 위아래로 흐르는 시간의 비율이 주어집니다. 우리의 임무는 흐름의 속도와 상하 흐름의 시간 비율에서 사람의 속도를 찾는 것입니다.

문제를 이해하기 위해 예를 들어 보겠습니다.

입력

S = 5, N = 2

출력

15

솔루션 접근 방식

문제에 대한 간단한 해결책은 조정 문제에 대한 수학 공식을 사용하는 것입니다. 공식이 어떻게 작동하는지 봅시다 -

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)}$$

우리 솔루션의 작동을 설명하는 프로그램

예시

#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;
}

출력

The speed of man is 24 km/hr