📌  相关文章
📜  最小化获得最多N的所有可能值所需的硬币

📅  最后修改于: 2021-04-17 19:17:44             🧑  作者: Mango

给定整数N ,任务是找到{ 1,2,5 }值硬币的最小数量,以便可以形成[1,N]范围内所有可能值的变化,并且不可能获得值N.

例子:

方法:可以使用贪婪技术解决问题。该想法基于以下观察:

请按照以下步骤解决问题:

  • 初始化三个变量,例如F,TO ,以存储5个2个1个值的硬币的计数。
  • 使用F =(N – 4)/ 5计算5值硬币的数量
  • 如果(N – 5 * F)是偶数,则可以计算出一枚有价值的硬币的计数为O = 1
  • 否则,可以将一枚有价值硬币的计数计算为O = 2
  • 计算两个有价硬币的计数可以计算为T =(N – 5 * F – O)/ 2。
  • 最后,打印F,TO的

下面是上述方法的实现:

C++
#include 
using namespace std;
 
// Funtion to find minimum count of {1, 2, 5}
// valued coins required to make a change of
// all values in the range [1, N]
void find(int N)
{
  int T, F, O;
 
  // Number of 5 valueds coins required
  F = int((N - 4) / 5);
 
  // Number of 1 valued coins required
  if (((N - 5 * F) % 2) == 0)
  {
    O = 2;
  }
 
  else
  {
    O = 1 ;
  }
 
  // Number of 2 valued coins required
  T = floor((N - 5 * F - O)/2);
 
  cout<< "Count of 5 valueds coins: " << F << endl;
  cout<< "Count of 2 valueds coins: " << T<< endl;
  cout<< "Count of 1 valueds coins: " << O << endl;
}
 
// Driver Code
int main()
{
  int N = 8;
  find(N);
  return 0;
}
 
// This code is contributed by Jana_sayantan.


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
// Funtion to find minimum count of {1, 2, 5}
// valued coins required to make a change of
// all values in the range [1, N]
static void find(int N)
{
  int T, F, O;
 
  // Number of 5 valueds coins required
  F = (int)((N - 4) / 5);
 
  // Number of 1 valued coins required
  if (((N - 5 * F) % 2) == 0)
  {
    O = 2;
  }
 
  else
  {
    O = 1 ;
  }
 
  // Number of 2 valued coins required
  T = (int)Math.floor((N - 5 * F - O)/2);
 
   System.out.println("Count of 5 valueds coins: " + F);
   System.out.println("Count of 2 valueds coins: " + T);
   System.out.println("Count of 1 valueds coins: " + O);
}
 
// Driver Code
public static void main(String args[])
{
    int N = 8;
    find(N);
}
}
 
// This code is contributed by splevel62.


Python3
# Python Program for the above approach
 
# Funtion to find minimum count of {1, 2, 5}
# valued coins required to make a change of
# all values in the range [1, N]
def find(N):
     
    # Number of 5 valueds coins required
    F = int((N - 4) / 5)
 
    # Number of 1 valued coins required
    if ((N - 5 * F) % 2) == 0:
        O = 2
 
    else:
        O = 1
 
    # Number of 2 valued coins required
    T = (N - 5 * F - O)//2
 
    print("Count of 5 valueds coins: ", F)
    print("Count of 2 valueds coins: ", T)
    print("Count of 1 valueds coins: ", O)
 
if __name__ == '__main__':
     
    N = 8
    find(N)


C#
// C# program to implement
// the above approach
using System;
public class GFG
{
 
// Funtion to find minimum count of {1, 2, 5}
// valued coins required to make a change of
// all values in the range [1, N]
static void find(int N)
{
  int T, F, O;
 
  // Number of 5 valueds coins required
  F = (int)((N - 4) / 5);
 
  // Number of 1 valued coins required
  if (((N - 5 * F) % 2) == 0)
  {
    O = 2;
  }
 
  else
  {
    O = 1 ;
  }
 
  // Number of 2 valued coins required
  T = (int)Math.Floor((double)(N - 5 * F - O)/2);
 
   Console.WriteLine("Count of 5 valueds coins: " + F);
   Console.WriteLine("Count of 2 valueds coins: " + T);
   Console.WriteLine("Count of 1 valueds coins: " + O);
}
 
// Driver Code
public static void Main(String []args)
{
    int N = 8;
    find(N);
}
}
 
// This code is contributed by 29AjayKumar


输出:
Count of 5 valueds coins:  0
Count of 2 valueds coins:  3
Count of 1 valueds coins:  2

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