在 C++ 中根据水流速度和上游与下游时间之比求人的速度
在这个问题中,我们给定两个值 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
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP