📌  相关文章
📜  给定类型的硬币可以购买的最大物品

📅  最后修改于: 2021-05-05 02:23:15             🧑  作者: Mango

给定三个整数XYZ ,它们表示购买某些物品的硬币数量。物品的费用如下:

Item type Cost
1 3 X coins
2 3 Y coins
3 3 Z coins
4 1 X coin + 1 Y coin + 1 Z coin

任务是找到可以使用给定数量的硬币购买的最大物品数量。

方法:可以购买的类型1类型2类型3的商品数量分别为X / 3Y / 3Z / 3 。现在,购买这些物品后,硬币的数量将减少,如X = X%3Y = Y%3Z = Z%3 。由于购买类型4的物品需要每种类型的硬币。因此,可以购买的类型4的总项目将是XYZ的最小值,结果将是从每种类型中购买的这些项目的总和。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
const int COST = 3;
  
// Function to find maximum fruits
// Can buy from given values of x, y, z.
int maxItems(int x, int y, int z)
{
  
    // Items of type 1 that can be bought
    int type1 = x / COST;
  
    // Update the coins
    x %= COST;
  
    // Items of type 2 that can be bought
    int type2 = y / COST;
  
    // Update the coins
    y %= COST;
  
    // Items of type 3 that can be bought
    int type3 = z / COST;
  
    // Update the coins
    z %= COST;
  
    // Items of type 4 that can be bought
    // To buy a type 4 item, a coin
    // of each type is required
    int type4 = min(x, min(y, z));
  
    // Total items that can be bought
    int maxItems = type1 + type2 + type3 + type4;
    return maxItems;
}
  
// Driver code
int main()
{
    int x = 4, y = 5, z = 6;
  
    cout << maxItems(x, y, z);
  
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
  
class GFG 
{
static int COST = 3;
  
// Function to find maximum fruits
// Can buy from given values of x, y, z.
static int maxItems(int x, int y, int z)
{
  
    // Items of type 1 that can be bought
    int type1 = x / COST;
  
    // Update the coins
    x %= COST;
  
    // Items of type 2 that can be bought
    int type2 = y / COST;
  
    // Update the coins
    y %= COST;
  
    // Items of type 3 that can be bought
    int type3 = z / COST;
  
    // Update the coins
    z %= COST;
  
    // Items of type 4 that can be bought
    // To buy a type 4 item, a coin
    // of each type is required
    int type4 = Math.min(x, Math.min(y, z));
  
    // Total items that can be bought
    int maxItems = type1 + type2 + type3 + type4;
    return maxItems;
}
  
// Driver code
public static void main (String[] args) 
{
    int x = 4, y = 5, z = 6;
    System.out.println(maxItems(x, y, z));
}
} 
  
// This code is contributed by @tushil


Python3
# Python3 implementation of the approach 
COST = 3; 
  
# Function to find maximum fruits 
# Can buy from given values of x, y, z. 
def maxItems(x, y, z) : 
  
    # Items of type 1 that can be bought 
    type1 = x // COST; 
  
    # Update the coins 
    x %= COST; 
  
    # Items of type 2 that can be bought 
    type2 = y // COST; 
  
    # Update the coins 
    y %= COST; 
  
    # Items of type 3 that can be bought 
    type3 = z // COST; 
  
    # Update the coins 
    z %= COST; 
  
    # Items of type 4 that can be bought 
    # To buy a type 4 item, a coin 
    # of each type is required 
    type4 = min(x, min(y, z)); 
  
    # Total items that can be bought 
    maxItems = type1 + type2 + type3 + type4; 
    return maxItems; 
  
# Driver code 
if __name__ == "__main__" : 
  
    x = 4; y = 5; z = 6; 
  
    print(maxItems(x, y, z)); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
  
class GFG
{
static int COST = 3;
  
// Function to find maximum fruits
// Can buy from given values of x, y, z.
static int maxItems(int x, int y, int z)
{
  
    // Items of type 1 that can be bought
    int type1 = x / COST;
  
    // Update the coins
    x %= COST;
  
    // Items of type 2 that can be bought
    int type2 = y / COST;
  
    // Update the coins
    y %= COST;
  
    // Items of type 3 that can be bought
    int type3 = z / COST;
  
    // Update the coins
    z %= COST;
  
    // Items of type 4 that can be bought
    // To buy a type 4 item, a coin
    // of each type is required
    int type4 = Math.Min(x, Math.Min(y, z));
  
    // Total items that can be bought
    int maxItems = type1 + type2 + type3 + type4;
    return maxItems;
}
  
// Driver code
static public void Main ()
{
    int x = 4, y = 5, z = 6;
      
    Console.Write (maxItems(x, y, z));
}
} 
  
// This code is contributed by ajit..


输出:
4