📜  获得 N 个给定类型的物品所需的最低价格

📅  最后修改于: 2022-05-13 01:58:11.782000             🧑  作者: Mango

获得 N 个给定类型的物品所需的最低价格

给定整数XYZ ,分别表示A、 B 和C三个项目的价格,以及两个整数ab ,表示两种项目的数量。任务是计算获得N种类型A、BC的物品所需的最低价格,其中A需要2种类型aB需要3种类型bC需要1种类型a1b类型。

例子:

方法:上述问题可以通过固定一个项目的数量并检查形成剩余项目所需的价格来解决。请按照以下步骤解决问题:

  • 初始化变量,例如minimum_bill以存储购买N件商品所需的最低价格。
  • 迭代范围C ,小于或等于N
  • 初始化变量,比如maxorders_A等于(a – orders_C) / 2maxorders_B等于(b – orders_C) / 3。
  • 检查maxorders_A + maxorders_B + maxorders_C是否小于N ,然后继续。
  • 如果X小于Y ,则找到orders_A作为min(maxorders_A, N – orders_C)orders_B作为N – orders_C – orders_A
  • 否则,找到orders_B作为min(maxorders_B, N – orders_C)orders_A作为N – orders_C – orders_B
  • 计算变量中所需的账单,比如账单,(X * orders_A) + (Y * orders_B) + (Z * orders_C)
  • 在每次迭代中,将minimum_bill更新为minimum_billbill的最小值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate the minimum price
void FindBill(int X, int Y, int Z, int N,
              int a, int b)
{
 
    // Stores the amount required
    int minimum_bill = 10000000;
 
    // Iterating over total number
    // of possible items of type C
    for (int orders_C = 0; orders_C <= N; orders_C++) {
 
        // If count of a and b are less
        // than minimum required of type C
        if (a < orders_C or b < orders_C)
            break;
 
        // Maximum number of orders of type A
        int maxorders_A = (a - orders_C) / 2;
 
        // Maximum number of orders of type +B
        int maxorders_B = (b - orders_C) / 3;
 
        // Initializing the required
        // number of orders for A and B
        int orders_A = 0;
        int orders_B = 0;
 
        // Total items should be
        // greater than or equal to N
        if ((maxorders_A + maxorders_B + orders_C) < N) {
            continue;
        }
 
        // If cost of A < cost of B
        if (X < Y) {
 
            // Total orders of A will be minimum
            // of maximum orders of A or just
            // remaining orders required
            orders_A = min(maxorders_A, N - orders_C);
 
            // Remaining number of orders for B
            orders_B = N - orders_C - orders_A;
        }
 
        // If cost of A > cost of B
        else {
 
            // Total orders of B will be minimum
            // of maximum orders of B or just
            // remaining orders required
            orders_B = min(maxorders_B, N - orders_C);
 
            // Remaining number of orders for A
            orders_A = N - orders_C - orders_B;
        }
 
        // Calculating bill
        int bill = (X * orders_A)
                   + (Y * orders_B)
                   + (Z * orders_C);
 
        // Updating minimum_bill
        minimum_bill = min(bill, minimum_bill);
    }
 
    // If ordering N items is not possible
    if (minimum_bill == 10000000)
        minimum_bill = 0;
 
    // Printing minimum bill
    cout << minimum_bill << endl;
}
 
// Driver Code
int main()
{
 
    // Given Input
    int X = 300, Y = 150, Z = 200, N = 5, a = 6, b = 4;
 
    /// Function Call
    FindBill(X, Y, Z, N, a, b);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
  // Function to calculate the minimum price
  static void FindBill(int X, int Y, int Z, int N,
                       int a, int b)
  {
 
    // Stores the amount required
    int minimum_bill = 10000000;
 
    // Iterating over total number
    // of possible items of type C
    for (int orders_C = 0; orders_C <= N; orders_C++) {
 
      // If count of a and b are less
      // than minimum required of type C
      if (a < orders_C || b < orders_C)
        break;
 
      // Maximum number of orders of type A
      int maxorders_A = (a - orders_C) / 2;
 
      // Maximum number of orders of type +B
      int maxorders_B = (b - orders_C) / 3;
 
      // Initializing the required
      // number of orders for A and B
      int orders_A = 0;
      int orders_B = 0;
 
      // Total items should be
      // greater than or equal to N
      if ((maxorders_A + maxorders_B + orders_C) < N) {
        continue;
      }
 
      // If cost of A < cost of B
      if (X < Y) {
 
        // Total orders of A will be minimum
        // of maximum orders of A or just
        // remaining orders required
        orders_A = Math.min(maxorders_A, N - orders_C);
 
        // Remaining number of orders for B
        orders_B = N - orders_C - orders_A;
      }
 
      // If cost of A > cost of B
      else {
 
        // Total orders of B will be minimum
        // of maximum orders of B or just
        // remaining orders required
        orders_B = Math.min(maxorders_B, N - orders_C);
 
        // Remaining number of orders for A
        orders_A = N - orders_C - orders_B;
      }
 
      // Calculating bill
      int bill = (X * orders_A)
        + (Y * orders_B)
        + (Z * orders_C);
 
      // Updating minimum_bill
      minimum_bill = Math.min(bill, minimum_bill);
    }
 
    // If ordering N items is not possible
    if (minimum_bill == 10000000)
      minimum_bill = 0;
 
    // Printing minimum bill
    System.out.println(minimum_bill);
  }
 
  // Driver Code
  public static void main (String[] args)
  {
 
    // Given Input
    int X = 300, Y = 150, Z = 200, N = 5, a = 6, b = 4;
 
    /// Function Call
    FindBill(X, Y, Z, N, a, b);
  }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python program for the above approach
 
# Function to calculate the minimum price
def FindBill(X, Y, Z, N, a, b):
  # Stores the amount required
  minimum_bill = 10000000;
 
  # Iterating over total number
  # of possible items of type C
  for orders_C in range(0, N + 1):
     
    # If count of a and b are less
    # than minimum required of type C
    if (a < orders_C or b < orders_C): break;
 
    # Maximum number of orders of type A
    maxorders_A = (a - orders_C) // 2;
 
    # Maximum number of orders of type +B
    maxorders_B = (b - orders_C) // 3;
 
    # Initializing the required
    # number of orders for A and B
    orders_A = 0;
    orders_B = 0;
 
    # Total items should be
    # greater than or equal to N
    if (maxorders_A + maxorders_B + orders_C < N):
      continue;
 
 
    # If cost of A < cost of B
    if (X < Y):
      # Total orders of A will be minimum
      # of maximum orders of A or just
      # remaining orders required
      orders_A = min(maxorders_A, N - orders_C);
 
      # Remaining number of orders for B
      orders_B = N - orders_C - orders_A;
     
 
    # If cost of A > cost of B
    else:
      # Total orders of B will be minimum
      # of maximum orders of B or just
      # remaining orders required
      orders_B = min(maxorders_B, N - orders_C);
 
      # Remaining number of orders for A
      orders_A = N - orders_C - orders_B;
     
 
    # Calculating bill
    bill = X * orders_A + Y * orders_B + Z * orders_C;
 
    # Updating minimum_bill
    minimum_bill = min(bill, minimum_bill);
   
 
  # If ordering N items is not possible
  if (minimum_bill == 10000000): minimum_bill = 0;
 
  # Printing minimum bill
  print(minimum_bill);
 
 
# Driver Code
 
# Given Input
X = 300
Y = 150
Z = 200
N = 5
a = 6
b = 4
 
#/ Function Call
FindBill(X, Y, Z, N, a, b);
 
# This code is contributed by gfgking.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to calculate the minimum price
static void FindBill(int X, int Y, int Z, int N,
              int a, int b)
{
 
    // Stores the amount required
    int minimum_bill = 10000000;
 
    // Iterating over total number
    // of possible items of type C
    for (int orders_C = 0; orders_C <= N; orders_C++) {
 
        // If count of a and b are less
        // than minimum required of type C
        if (a < orders_C || b < orders_C)
            break;
 
        // Maximum number of orders of type A
        int maxorders_A = (a - orders_C) / 2;
 
        // Maximum number of orders of type +B
        int maxorders_B = (b - orders_C) / 3;
 
        // Initializing the required
        // number of orders for A and B
        int orders_A = 0;
        int orders_B = 0;
 
        // Total items should be
        // greater than or equal to N
        if ((maxorders_A + maxorders_B + orders_C) < N) {
            continue;
        }
 
        // If cost of A < cost of B
        if (X < Y) {
 
            // Total orders of A will be minimum
            // of maximum orders of A or just
            // remaining orders required
            orders_A = Math.Min(maxorders_A, N - orders_C);
 
            // Remaining number of orders for B
            orders_B = N - orders_C - orders_A;
        }
 
        // If cost of A > cost of B
        else {
 
            // Total orders of B will be minimum
            // of maximum orders of B or just
            // remaining orders required
            orders_B = Math.Min(maxorders_B, N - orders_C);
 
            // Remaining number of orders for A
            orders_A = N - orders_C - orders_B;
        }
 
        // Calculating bill
        int bill = (X * orders_A)
                   + (Y * orders_B)
                   + (Z * orders_C);
 
        // Updating minimum_bill
        minimum_bill = Math.Min(bill, minimum_bill);
    }
 
    // If ordering N items is not possible
    if (minimum_bill == 10000000)
        minimum_bill = 0;
 
    // Printing minimum bill
    Console.Write(minimum_bill);
}
 
// Driver Code
public static void Main()
{
 
    // Given Input
    int X = 300, Y = 150, Z = 200, N = 5, a = 6, b = 4;
 
    /// Function Call
    FindBill(X, Y, Z, N, a, b);
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出:
1100

时间复杂度: O(N)
辅助空间: O(1)