给定三个整数X , Y和Z ,它们表示购买某些物品的硬币数量。物品的费用如下:
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 |
任务是找到可以使用给定数量的硬币购买的最大物品数量。
Input: X = 4, Y = 5, Z = 6
Output: 4
Buy 1 item of type 1: X = 1, Y = 5, Z = 6
Buy 1 item of type 2: X = 1, Y = 2, Z = 6
Buy 2 items of type 3: X = 1, Y = 2, Z = 0
Total items bought = 1 + 1 + 2 = 4
Input: X = 6, Y = 7, Z = 9
Output: 7
方法:可以购买的类型1 ,类型2和类型3的商品数量分别为X / 3 , Y / 3和Z / 3 。现在,购买这些物品后,硬币的数量将减少,如X = X%3 , Y = Y%3和Z = Z%3 。由于购买类型4的物品需要每种类型的硬币。因此,可以购买的类型4的总项目将是X , Y和Z的最小值,结果将是从每种类型中购买的这些项目的总和。
下面是上述方法的实现:
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