📌  相关文章
📜  检查给定的硬币是否可用于支付价值 S

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

给定值M的值N A硬币和B硬币,任务是检查如果给硬币可以用来支付S的值。

例子:

方法:
这个想法是使用贪婪的方法。

  • 继续从所需的总和 S 中减去价值 N 的硬币。
  • 在每一步中,在减去价值 N 的硬币时,检查剩余的总和是否是价值 M 的硬币的倍数,并且我们有足够的价值 M 的硬币来获得剩余的总和。
  • 如果在任何一步,满足上述两个条件,则返回 YES。

下面是上述方法的实现:

C++
// C++ implementation to check
// if it is possible to pay a value
 
#include 
using namespace std;
 
// Function to check if it
// is possible to pay a value
void knowPair(int a, int b,
        int n, int s, int m){
     
    int i = 0, rem = 0;
    int count_b = 0, flag = 0;
 
    // Loop to add the value of coin A
    while (i <= a) {
        rem = s - (n * i);
        count_b = rem / m;
        if (rem % m == 0 && count_b <= b){
            flag = 1;
        }
        i++;
    }
     
    // Condition to check if it is
    // possible to pay a value of S
    if (flag == 1) {
        cout << "YES" << endl;
    }else{
        cout << "NO" << endl;
    }
}
 
// Driver Code
int main()
{
    int A = 1;
    int B = 2;
    int n = 3;
    int S = 4;
    int m = 2;
 
    knowPair(A, B, n, S, m);
 
    return 0;
}


Java
// Java implementation to check
// if it is possible to pay a value
class GFG{
  
// Function to check if it
// is possible to pay a value
static void knowPair(int a, int b,
        int n, int s, int m){
      
    int i = 0, rem = 0;
    int count_b = 0, flag = 0;
  
    // Loop to add the value of coin A
    while (i <= a) {
        rem = s - (n * i);
        count_b = rem / m;
        if (rem % m == 0 && count_b <= b){
            flag = 1;
        }
        i++;
    }
      
    // Condition to check if it is
    // possible to pay a value of S
    if (flag == 1) {
        System.out.print("YES" +"\n");
    }else{
        System.out.print("NO" +"\n");
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int A = 1;
    int B = 2;
    int n = 3;
    int S = 4;
    int m = 2;
  
    knowPair(A, B, n, S, m);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 implementation to check
# if it is possible to pay a value
 
# Function to check if it
# is possible to pay a value
def knowPair(a,b,n,s,m):
    i = 0
    rem = 0
    count_b = 0
    flag = 0
 
    # Loop to add the value of coin A
    while (i <= a):
        rem = s - (n * i)
        count_b = rem // m
        if (rem % m == 0 and count_b <= b):
            flag = 1
        i += 1
     
    # Condition to check if it is
    # possible to pay a value of S
    if (flag == 1):
        print("YES")
    else:
        print("NO")
 
# Driver Code
if __name__ == '__main__':
    A = 1
    B = 2
    n = 3
    S = 4
    m = 2
 
    knowPair(A, B, n, S, m)
     
# This code is contributed by Surendra_Gangwar


C#
// C# implementation to check
// if it is possible to pay a value
using System;
 
class GFG{
   
// Function to check if it
// is possible to pay a value
static void knowPair(int a, int b,
        int n, int s, int m){
       
    int i = 0, rem = 0;
    int count_b = 0, flag = 0;
   
    // Loop to add the value of coin A
    while (i <= a) {
        rem = s - (n * i);
        count_b = rem / m;
        if (rem % m == 0 && count_b <= b){
            flag = 1;
        }
        i++;
    }
       
    // Condition to check if it is
    // possible to pay a value of S
    if (flag == 1) {
        Console.Write("YES" + "\n");
    }else{
        Console.Write("NO" + "\n");
    }
}
   
// Driver Code
public static void Main(String[] args)
{
    int A = 1;
    int B = 2;
    int n = 3;
    int S = 4;
    int m = 2;
   
    knowPair(A, B, n, S, m);
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
YES