📌  相关文章
📜  分两步可以提取的最大金额

📅  最后修改于: 2021-05-04 23:54:28             🧑  作者: Mango

有两个现金储物柜,一个存有X个硬币,另一个存有Y个硬币,您最多可以提取两次,从储物柜中取出时,您将获得储物柜的总金额,并且如果最初有Z个硬币,则重新填充Z – 1个硬币。任务是找到您可以获得的最大硬币数。

例子:

方法:为了最大化硬币数量,请从具有最大值的储物柜中取出,然后更新储物柜,然后再次从具有最大值的储物柜中取出。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the maximum coins we can get
int maxCoins(int X, int Y)
{
  
    // Update elements such that X > Y
    if (X < Y)
        swap(X, Y);
  
    // Take from the maximum
    int coins = X;
  
    // Refill
    X--;
  
    // Again, take the maximum
    coins += max(X, Y);
  
    return coins;
}
  
// Driver code
int main()
{
  
    int X = 7, Y = 5;
  
    cout << maxCoins(X, Y);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG {
    // Function to return the maximum coins we can get
    static int maxCoins(int X, int Y)
    {
  
        // Update elements such that X > Y
        if (X < Y) {
            swap(X, Y);
        }
  
        // Take from the maximum
        int coins = X;
  
        // Refill
        X--;
  
        // Again, take the maximum
        coins += Math.max(X, Y);
  
        return coins;
    }
  
    static void swap(int X, int Y)
    {
        int temp = X;
        X = Y;
        Y = temp;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int X = 7, Y = 5;
        System.out.println(maxCoins(X, Y));
    }
}
  
// This code has been contributed by 29AjayKumar


Python3
# Python3 implementation of the approach 
  
# Function to return the maximum coins we can get 
def maxCoins(X, Y) : 
  
    # Update elements such that X > Y 
    if (X < Y) : 
        X, Y = Y, X;
  
    # Take from the maximum 
    coins = X; 
  
    # Refill 
    X -= 1; 
  
    # Again, take the maximum 
    coins += max(X, Y); 
  
    return coins; 
  
  
# Driver code 
if __name__ == "__main__" : 
  
    X = 7; Y = 5; 
  
    print(maxCoins(X, Y));
      
    # This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
  
class GFG {
    // Function to return the maximum coins we can get
    static int maxCoins(int X, int Y)
    {
  
        // Update elements such that X > Y
        if (X < Y) {
            swap(X, Y);
        }
  
        // Take from the maximum
        int coins = X;
  
        // Refill
        X--;
  
        // Again, take the maximum
        coins += Math.Max(X, Y);
  
        return coins;
    }
  
    static void swap(int X, int Y)
    {
        int temp = X;
        X = Y;
        Y = temp;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int X = 7, Y = 5;
        Console.WriteLine(maxCoins(X, Y));
    }
}
  
/* This code contributed by PrinciRaj1992 */


PHP
 Y 
    if ($X < $Y) 
        swap($X, $Y); 
  
    // Take from the maximum 
    $coins = $X; 
  
    // Refill 
    $X--; 
  
    // Again, take the maximum 
    $coins += max($X, $Y); 
  
    return $coins; 
} 
  
// Driver code 
  
$X = 7;
$Y = 5; 
  
echo maxCoins($X, $Y); 
  
// This code is contributed by Naman_Garg.
  
?>


输出:
13