📜  谜题 29 | (车轮拼图)

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

谜题 29 | (车轮拼图)

谜题一辆汽车有 4 个轮胎和 1 个备胎。每个轮胎在磨损前最多可以行驶 20000 英里。在您被迫购买新轮胎之前,汽车可以行驶的最大距离是多少?您可以无限次更换轮胎(使用备用轮胎)。

答案 25000公里
解决方法:将备胎的寿命分成4等份,即5000,每跑完5000英里就更换一次。
设四个轮胎分别命名为 A、B、C、D,备胎为 S。

  1. 5000 公里:用 S 替换 A。剩余距离(A、B、C、D、S):15000、15000、15000、15000、20000。

  1. 10000 KMs:将A放回原位,用S替换B。剩余距离(A、B、C、D、S):15000、10000、10000、10000、15000。



  1. 15000 KMs:将B放回原位,用S替换C。剩余距离(A、B、C、D、S):10000、10000、5000、5000、10000。

  1. 20000 KMs:将C放回原位,用S替换D。剩余距离(A、B、C、D、S):5000、5000、5000、0、5000。

  1. 25000 公里:现在每个轮胎都完全磨损了。

所有轮胎都充分发挥其强度。

相关亚马逊面试题

有 n 支铅笔,每支铅笔的长度为 l。每个可以写4公里。写入 4 公里后,长度为 l/4。一个可以连接 4 支长度为 l/4 的铅笔,可以制作 1 支铅笔。如果剩余的碎片数量为 3 或 2 或 1 个,则不能制作铅笔碎片,但可以在需要时包括这些剩余的碎片。
写出一个与给定铅笔的长度 l 无关的关系,表示一个人可以用 n 支铅笔写出多少。
例子:

Input: 4
Output: 20



递归方法:
假设我们使用 3 支铅笔,这将是 12 支并生成 3 支用过的铅笔,现在如果剩余的铅笔大于零,则至少可以使用 1 支未使用的铅笔,而这 3 支未使用的铅笔可以用来写 4,这将生成另外 1 支未使用的铅笔。这将不断重复。

if(n-3 >= 1){
  f(n) = f(n-3) + 12 + 4
} 
else{
  // Used pencil that cannot be used
  f(n) = 4 + 4 
}

下面是上述方法的实现:

C
int count(int n)
{
    if (n < 4) {
        return (n * 4);
    }
    else {
        return (16 + count(n - 3));
    }
}


CPP
// C++ program to find relation independent of l
// length of the given pencil, for how much one
// can write from n pencils.
 
#include 
using namespace std;
 
// Function to find no of pencils
int count(int n)
{
    int x = (n / 3) - 1;
    if (n % 3) {
        x++;
    }
    return (4 * x + 4 * n);
}
 
// Driver function
int main()
{
    int n = 5;
    cout << count(n) << endl;
}


Java
// Java program to find relation independent of l
// length of the given pencil, for how much one
// can write from n pencils.
class GFG
{
 
// Function to find no of pencils
static int count(int n)
{
    int x = (n / 3) - 1;
    if (n % 3 > 0)
    {
        x++;
    }
    return (4 * x + 4 * n);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
    System.out.print(count(n) +"\n");
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to find relation independent of l
# length of the given pencil, for how much one
# can write from n pencils.
 
# Function to find no of pencils
def count(n):
    x = (n // 3) - 1;
    if (n % 3 > 0):
        x+=1;
     
    return (4 * x + 4 * n);
 
# Driver code
if __name__ == '__main__':
    n = 5;
    print(count(n));
     
# This code is contributed by PrinciRaj1992


C#
// C# program to find relation independent of l
// length of the given pencil, for how much one
// can write from n pencils.
using System;
 
class GFG
{
 
// Function to find no of pencils
static int count(int n)
{
    int x = (n / 3) - 1;
    if (n % 3 > 0)
    {
        x++;
    }
    return (4 * x + 4 * n);
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 5;
    Console.Write(count(n) +"\n");
}
}
 
// This code is contributed by 29AjayKumar


Javascript


数学方法 O(1):
上述关系可以在 O(1) 中优化

// x is max no of time we can 
//subtract 3 without n-3 <= 3
n - 3 + x <= 3     
x  >  (n-3)/3
i.e. n/3 - 1 if it divides exactly
else n/3

下面是上述方法的实现:

CPP

// C++ program to find relation independent of l
// length of the given pencil, for how much one
// can write from n pencils.
 
#include 
using namespace std;
 
// Function to find no of pencils
int count(int n)
{
    int x = (n / 3) - 1;
    if (n % 3) {
        x++;
    }
    return (4 * x + 4 * n);
}
 
// Driver function
int main()
{
    int n = 5;
    cout << count(n) << endl;
}

Java

// Java program to find relation independent of l
// length of the given pencil, for how much one
// can write from n pencils.
class GFG
{
 
// Function to find no of pencils
static int count(int n)
{
    int x = (n / 3) - 1;
    if (n % 3 > 0)
    {
        x++;
    }
    return (4 * x + 4 * n);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
    System.out.print(count(n) +"\n");
}
}
 
// This code is contributed by PrinciRaj1992

蟒蛇3

# Python3 program to find relation independent of l
# length of the given pencil, for how much one
# can write from n pencils.
 
# Function to find no of pencils
def count(n):
    x = (n // 3) - 1;
    if (n % 3 > 0):
        x+=1;
     
    return (4 * x + 4 * n);
 
# Driver code
if __name__ == '__main__':
    n = 5;
    print(count(n));
     
# This code is contributed by PrinciRaj1992

C#

// C# program to find relation independent of l
// length of the given pencil, for how much one
// can write from n pencils.
using System;
 
class GFG
{
 
// Function to find no of pencils
static int count(int n)
{
    int x = (n / 3) - 1;
    if (n % 3 > 0)
    {
        x++;
    }
    return (4 * x + 4 * n);
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 5;
    Console.Write(count(n) +"\n");
}
}
 
// This code is contributed by 29AjayKumar

Javascript


输出:
24