📜  分两步可提取的最大金额

📅  最后修改于: 2021-10-26 06:36:08             🧑  作者: 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.
 
?>


Javascript


输出:
13

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程