📌  相关文章
📜  最大限度地减少购买对象的成本

📅  最后修改于: 2021-05-07 09:54:48             🧑  作者: Mango

给定“ T” ,代表一个人拥有的物品总数。 “ P”代表每件商品的价格, “ M”代表他购买“ N”件商品可获得的免费商品数量。任务是找到该人为获得T物品而必须支付的总金额。
例子:

方法:

  1. 首先,我们应该尝试获得尽可能多的免费物品,这样可以降低成本。
  2. 将项目总数除以NM之和,因为当我们购买至少N个项目时,我们得到M个免费项目。
  3. 然后通过从项目总数中减去免费项目来计算您必须支付的项目总数
  4. 最后,可以通过将一件商品的成本乘以一件商品的总数来计算价格。

下面是上述方法的实现:

C++
// C++ program of above approach
#include 
using namespace std;
 
// Function that will calculate the price
int totalPay(int totalItems, int priceOfOneItem,
             int N, int M)
{
    int freeItems = 0, actual = 0;
 
    // Calculate the number of items
    // we can get for free
    freeItems = totalItems / (N + M);
 
    // Calculate the number of items
    // we will have to pay the price for
    actual = totalItems - freeItems;
 
    // Calculate the price
    int amount = actual * priceOfOneItem;
 
    return amount;
}
 
// Driver code
int main()
{
    int T = 12, P = 8;
    int N = 2, M = 1;
 
    // Calling function
    cout << "Amount = "
         << totalPay(T, P, N, M);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
// Function that will calculate the price
static int totalPay(int totalItems,
                    int priceOfOneItem,
                    int N, int M)
{
    int freeItems = 0, actual = 0;
 
    // Calculate the number of items
    // we can get for free
    freeItems = totalItems / (N + M);
 
    // Calculate the number of items
    // we will have to pay the price for
    actual = totalItems - freeItems;
 
    // Calculate the price
    int amount = actual * priceOfOneItem;
 
    return amount;
}
 
// Driver code
public static void main(String[] args)
{
    int T = 12, P = 8;
    int N = 2, M = 1;
 
    // Calling function
    System.out.print("Amount = " +
                      totalPay(T, P, N, M));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program of above approach
 
# Function that will calculate the price
def totalPay(totalItems, priceOfOneItem, N, M):
    freeItems = 0
    actual = 0
 
    # Calculate the number of items
    # we can get for free
    freeItems = totalItems // (N + M)
 
    # Calculate the number of items
    # we will have to pay the price for
    actual = totalItems - freeItems
 
    # Calculate the price
    amount = actual * priceOfOneItem
 
    return amount
 
# Driver code
T = 12
P = 8
N = 2
M = 1
 
# Calling function
print("Amount = ", totalPay(T, P, N, M))
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
class GFG
{
 
// Function that will calculate the price
static int totalPay(int totalItems,
                    int priceOfOneItem,
                    int N, int M)
{
    int freeItems = 0, actual = 0;
 
    // Calculate the number of items
    // we can get for free
    freeItems = totalItems / (N + M);
 
    // Calculate the number of items
    // we will have to pay the price for
    actual = totalItems - freeItems;
 
    // Calculate the price
    int amount = actual * priceOfOneItem;
 
    return amount;
}
 
// Driver code
public static void Main(String[] args)
{
    int T = 12, P = 8;
    int N = 2, M = 1;
 
    // Calling function
    Console.Write("Amount = " +
                   totalPay(T, P, N, M));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
Amount = 64