给定N个怪兽,每个怪兽的初始生命值h [i]为整数。如果怪物的生命值大于0,则它仍然存在。在每一回合中,随机怪物杀死另一个随机怪物,即被攻击的怪物,其生命值会受到攻击怪物的生命值的影响而降低。这个过程一直持续到剩下一个怪物为止。最后剩下的怪物的最小可能生命值是多少。换句话说,任务是以这样的方式进行游戏:最终剩下的怪物的健康状况最低。
例子:
Input: h[] = {2, 14, 28, 56}
Output: 2
When only the first monster keeps on attacking the remaining 3 monsters, the final health of the last monster will be 2, which is minimum.
Input: h[] = {7, 17, 9, 100, 25}
Output: 1
Input: h[] = {5, 5, 5}
Output: 5
方法:从这个问题可以看出,一个人必须找到某种怪物的生命值,比方说,k可以杀死包括自我在内的其他怪物。一旦做出了至关重要的观察,问题就变得容易了。假设我们有两个怪兽,它们的生命值分别为h1和h2 ,例如h2> h1 。我们可以看到,在随机选择中,最佳方法是选择一个生命值较低的怪物,并降低另一个怪物的生命值,直到其生命值小于攻击怪物的生命值为止。此后,我们将选择第二只生命值小于h1的怪物,该过程将继续进行,直到只剩下一个怪物为止。因此,最后我们将剩下最小值gcd(h1,h2) 。这种gcd方法可以扩展到所有怪物。
因此,我们得出的怪物的最小可能健康状况将是给定怪物所有健康状况的gcd,即H(min)= gcd(h1,h2,…,hn)。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the gcd of two numbers
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to return the minimum
// possible health for the monster
int solve(int* health, int n)
{
// gcd of first and second element
int currentgcd = gcd(health[0], health[1]);
// gcd for all subsequent elements
for (int i = 2; i < n; ++i) {
currentgcd = gcd(currentgcd, health[i]);
}
return currentgcd;
}
// Driver code
int main()
{
int health[] = { 4, 6, 8, 12 };
int n = sizeof(health) / sizeof(health[0]);
cout << solve(health, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to return the gcd of two numbers
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to return the minimum
// possible health for the monster
static int solve(int health[], int n)
{
// gcd of first and second element
int currentgcd = gcd(health[0], health[1]);
// gcd for all subsequent elements
for (int i = 2; i < n; ++i)
{
currentgcd = gcd(currentgcd, health[i]);
}
return currentgcd;
}
// Driver code
public static void main(String args[])
{
int health[] = { 4, 6, 8, 12 };
int n = health.length;
System.out.println(solve(health, n));
}
}
// This code is contributed by
// Surendra_Gangwar
Python3
# Python3 implementation of the approach
# Function to return the gcd of two numbers
def gcd(a, b):
if (a == 0):
return b
return gcd(b % a, a)
# Function to return the minimum
# possible health for the monster
def solve(health, n):
# gcd of first and second element
currentgcd = gcd(health[0], health[1])
# gcd for all subsequent elements
for i in range(2, n):
currentgcd = gcd(currentgcd,
health[i])
return currentgcd
# Driver code
health = [4, 6, 8, 12]
n = len(health)
print(solve(health, n))
# This code is contributed by mohit kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the gcd of two numbers
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to return the minimum
// possible health for the monster
static int solve(int []health, int n)
{
// gcd of first and second element
int currentgcd = gcd(health[0], health[1]);
// gcd for all subsequent elements
for (int i = 2; i < n; ++i)
{
currentgcd = gcd(currentgcd, health[i]);
}
return currentgcd;
}
// Driver code
public static void Main(String []args)
{
int []health = { 4, 6, 8, 12 };
int n = health.Length;
Console.WriteLine(solve(health, n));
}
}
// This code is contributed by Arnab Kundu
PHP
2