有n辆自行车,加油后每辆可以行驶100公里。您使用n辆自行车可以行驶的最大距离是多少?您可能会假设所有自行车都是相似的,并且一辆自行车需要1升才能覆盖1公里。
您有n辆自行车,而使用一辆自行车则只能行驶100公里。因此,如果n辆自行车从同一点开始并同时行驶,那么您只能行驶100公里。让我们换个角度来看,技巧是当您想要覆盖最大距离时,您应该始终尝试浪费最少的燃料。最少的燃料浪费意味着要运行最少数量的自行车。您可以考虑连续运行它们,而不是并行运行n辆自行车。这意味着,如果您将一定量的燃料从最后一辆自行车转移到另一辆自行车上,然后扔掉最后一辆自行车,即不要在特定时间点之后再运行最后一辆自行车。但是问题是,必须经过多远的距离才能进行燃油传输,以便覆盖最大距离并且剩余自行车的油箱不会溢出。
让我们采用以下基本情况,然后对解决方案进行概括。
- 基本案例1:有一辆自行车:这很简单,我们只能行驶100公里。
- 基本案例2:有两辆自行车:当有两辆自行车时,我们可以覆盖的最大距离是多少?为了使距离最大化,我们必须在某个时候放下第二辆自行车,并将其燃料转移到第一辆自行车上。让我们在x公里后进行传输。
Total distance covered = Distance covered by 100 ltr in first bike +
Distance covered by fuel transferred from
first bike.
- 第二辆自行车的剩余燃油为100 – x。如果我们将如此多的燃料转移到第一辆自行车上,则总距离将变为100 + 100 – x,即200 – x。因此,我们的任务是最大化200-x。限制条件是100 – x必须小于或等于x公里后第一个自行车中创建的空间,即100 – x <= x。当x最小时,200-x的值变为最大。 x的最小可能值为50。因此我们能够行驶150公里。
- 基本案例3:有3辆自行车:让第一次转移在x公里之后完成。 x距离后,所有自行车都包含100-x量的燃料。如果我们从第3辆自行车中提取100倍的燃油,并在第1辆和第2辆自行车之间分配燃油,以使第1辆和第2辆自行车的油箱充满。所以100-x <= 2 * x;或者,x = 33.333,因此我们应该转移第三辆自行车的剩余燃料,并在精确地33.33公里之后在第一和第二辆自行车中分配该数量的燃料。
让我们概括一下。如果我们仔细研究上述情况,可以观察到,如果有n辆自行车,则在100 / n kms之后完成第一次转移(或掉落一辆自行车)。更概括地说,当我们每辆自行车有x升的剩余燃料,而n辆剩余的自行车有x升的燃料时,我们会在x / n kms之后放下一辆自行车。
以下是一般函数的实现。
C++
#include
// Returns maximum distance that can be traveled by n bikes and given fuel
// in every bike
double maxDistance(int n, int fuel)
{
// dist_covered is the result of this function
double dist_covered = 0;
while (n > 0)
{
// after ever fuel/n km we are discarding one bike and filling
// all the other bikes with fuel/n liters of fuel i.e. to their
// maximum limit (100 litre)
dist_covered += (double)fuel / n;
n -= 1; // reduce number of bikes
}
return dist_covered;
}
// Driver program to test above function
int main()
{
int n = 3; // number of bikes
int fuel = 100;
printf("Maximum distance possible with %d bikes is %f",
n, maxDistance(n, fuel));
return 0;
}
Java
// Java program to find the maximum
// distance covered using n bikes
import java.io.*;
class GFG
{
// Function that returns maximum distance that can be traveled by n bikes
// and given fuel in every bike
static double maxDistance(int n, int fuel)
{
// dist_covered is the result of this function
double dist_covered = 0;
while (n > 0)
{
// after ever fuel/n km we are discarding one bike and filling
// all the other bikes with fuel/n liters of fuel i.e. to their
// maximum limit (100 litre)
dist_covered += (double)fuel / n;
n -= 1; // reduce number of bikes
}
return dist_covered;
}
// driver program
public static void main (String[] args)
{
int n = 3; // number of bikes
int fuel = 100;
System.out.println("Maximum distance possible with "
+ n + " bikes is " + maxDistance(n, fuel));
}
}
// Contributed by Pramod Kumar
Python3
# Python 3 program to find the maximum
# distance covered using n bikes
# Returns maximum distance that can be
# traveled by n bikes and given fuel
# in every bike
def maxDistance(n, fuel):
# dist_covered is the result
# of this function
dist_covered = 0
while (n > 0):
# after ever fuel/n km we are
# discarding one bike and filling
# all the other bikes with fuel/n
# liters of fuel i.e. to their
# maximum limit (100 litre)
dist_covered = dist_covered + (fuel / n)
# reduce number of bikes
n = n - 1
return dist_covered
# Driver Code
if __name__ =='__main__':
n = 3
# number of bikes
fuel = 100
print("Maximum distance possible with",
n, "bikes is", maxDistance(n, fuel))
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to find the maximum
// distance covered using n bikes
using System;
class GFG {
// Function that returns maximum distance
// that can be travelled by n bikes
// and given fuel in every bike
static double maxDistance(int n, int fuel)
{
// dist_covered is the result of this function
double dist_covered = 0;
while (n > 0) {
// after ever fuel/n km we are discarding
// one bike and filling all the other bikes
// with fuel/n liters of fuel i.e. to their
// maximum limit (100 litre)
dist_covered += (double)fuel / n;
n -= 1; // reduce number of bikes
}
return dist_covered;
}
// driver program
public static void Main()
{
// number of bikes
int n = 3;
int fuel = 100;
Console.WriteLine("Maximum distance possible with " + n +
" bikes is " + maxDistance(n, fuel));
}
}
// This code is contributed by Sam007
PHP
0)
{
// after ever fuel/n km we are
// discarding one bike and filling
// all the other bikes with fuel/n
// liters of fuel i.e. to their
// maximum limit (100 litre)
$dist_covered += (double)$fuel / $n;
// reduce number of bikes
$n -= 1;
}
return $dist_covered;
}
// Driver Code
// number of bikes
$n = 3;
$fuel = 100;
echo "Maximum distance possible with ",
$n, " bikes is ",
maxDistance($n, $fuel);
// This code is contributed by ajit
?>
Javascript
输出 :
Maximum distance possible with 3 bikes is 183.333333