📌  相关文章
📜  购买最低数量的零钱,并给予硬币

📅  最后修改于: 2021-06-26 23:59:50             🧑  作者: Mango

您有无限数量的10卢比硬币和1卢比硬币,并且您需要购买最小成本为k的物品,以便您不要求找零。

例子:

显然,我们可以支付10件物品而无需任何更改(通过支付所需的10卢比硬币金额,而不使用r卢比硬币)。但是也许您可以购买更少的锤子,而无需支付任何费用。请注意,您应该至少购买一件商品。

C++
#include 
using namespace std;
  
int minItems(int k, int r)
{
   // See if we can buy less than 10 items
   // Using 10 Rs coins and one r Rs coin
   for (int i = 1; i < 10; i++) 
        if ((i * k - r) % 10 == 0 ||
            (i * k) % 10 == 0) 
            return i;
  
    // We can always buy 10 items
    return 10;
}
  
int main()
{
    int k = 15;
    int r = 2;
    cout << minItems(k, r);
    return 0;
}


Java
// Java implementation of above approach
import java.util.*;
  
class GFG
{
static int minItems(int k, int r)
{
      
// See if we can buy less than 10 items
// Using 10 Rs coins and one r Rs coin
for (int i = 1; i < 10; i++) 
        if ((i * k - r) % 10 == 0 ||
            (i * k) % 10 == 0) 
            return i;
  
    // We can always buy 10 items
    return 10;
}
  
// Driver Code
public static void main(String args[])
{
    int k = 15;
    int r = 2;
    System.out.println(minItems(k, r));
}
}
  
// This code is contributed 
// by SURENDRA_GANGWAR


Python3
# Python3 implementation of above approach
  
def minItems(k, r) :
  
    # See if we can buy less than 10 items 
    # Using 10 Rs coins and one r Rs coin 
    for i in range(1, 10) : 
            if ((i * k - r) % 10 == 0 or 
                (i * k) % 10 == 0) :
                return i 
      
    # We can always buy 10 items 
    return 10; 
      
# Driver Code
if __name__ == "__main__" : 
  
    k, r = 15 , 2; 
    print(minItems(k, r))
  
# This code is contributed by Ryuga


C#
// C# implementation of above approach
using System;
  
class GFG
{
static int minItems(int k, int r)
{
      
// See if we can buy less than 10 items
// Using 10 Rs coins and one r Rs coin
for (int i = 1; i < 10; i++) 
        if ((i * k - r) % 10 == 0 ||
            (i * k) % 10 == 0) 
            return i;
  
    // We can always buy 10 items
    return 10;
}
  
// Driver Code
public static void Main()
{
    int k = 15;
    int r = 2;
    Console.WriteLine(minItems(k, r));
}
}
  
// This code is contributed 
// by inder_verma


PHP


输出:
2

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。