📌  相关文章
📜  检查是否可以交换

📅  最后修改于: 2021-04-23 07:18:49             🧑  作者: Mango

给定一个国家中N个类型的货币面额arr [] 。一个人和店主可以多次选择每种面额的钞票。任务是检查当该人想要从商店购买价值为P的产品时是否可以进行兑换。

例子:

方法:为了能够进行兑换, G必须将所有面额的G整除P。因此,找到数组元素的gcd并检查P是否可被其整除。

下面是该方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if
// the exchange is possible
bool isPossible(int arr[], int n, int p)
{
  
    // Find the GCD of the array elements
    int gcd = 0;
    for (int i = 0; i < n; i++)
        gcd = __gcd(gcd, arr[i]);
  
    // If the exchange is possible
    if (p % gcd == 0)
        return true;
  
    return false;
}
  
// Driver code
int main()
{
    int arr[] = { 6, 9 };
    int n = sizeof(arr) / sizeof(int);
    int p = 3;
  
    if (isPossible(arr, n, p))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach 
class GFG 
{
      
    // Recursive function to 
    // return gcd of a and b 
    static int __gcd(int a, int b) 
    { 
        if (b == 0) 
            return a; 
        return __gcd(b, a % b); 
    } 
      
    // Function that returns true if 
    // the exchange is possible 
    static boolean isPossible(int []arr, 
                              int n, int p) 
    { 
      
        // Find the GCD of the array elements 
        int gcd = 0; 
        for (int i = 0; i < n; i++) 
            gcd = __gcd(gcd, arr[i]); 
      
        // If the exchange is possible 
        if (p % gcd == 0) 
            return true; 
      
        return false; 
    } 
      
    // Driver code 
    public static void main (String[] args) 
    { 
        int arr[] = { 6, 9 }; 
        int n = arr.length; 
        int p = 3; 
      
        if (isPossible(arr, n, p)) 
            System.out.println("Yes"); 
        else
            System.out.println("No"); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach 
from math import gcd as __gcd
  
# Function that returns true if 
# the exchange is possible 
def isPossible(arr, n, p) : 
  
    # Find the GCD of the array elements 
    gcd = 0; 
    for i in range(n) :
        gcd = __gcd(gcd, arr[i]); 
  
    # If the exchange is possible 
    if (p % gcd == 0) :
        return True; 
  
    return False;
  
# Driver code 
if __name__ == "__main__" : 
  
    arr = [ 6, 9 ]; 
    n = len(arr); 
    p = 3; 
  
    if (isPossible(arr, n, p)) :
        print("Yes"); 
    else :
        print("No"); 
          
# This code is contributed by kanugargng


C#
// C# implementation of the approach 
using System;
  
class GFG 
{
      
    // Recursive function to 
    // return gcd of a and b 
    static int __gcd(int a, int b) 
    { 
        if (b == 0) 
            return a; 
        return __gcd(b, a % b); 
    } 
      
    // Function that returns true if 
    // the exchange is possible 
    static bool isPossible(int []arr, 
                           int n, int p) 
    { 
      
        // Find the GCD of the array elements 
        int gcd = 0; 
        for (int i = 0; i < n; i++) 
            gcd = __gcd(gcd, arr[i]); 
      
        // If the exchange is possible 
        if (p % gcd == 0) 
            return true; 
      
        return false; 
    } 
      
    // Driver code 
    public static void Main (String[] args) 
    { 
        int []arr = { 6, 9 }; 
        int n = arr.Length; 
        int p = 3; 
      
        if (isPossible(arr, n, p)) 
            Console.WriteLine("Yes"); 
        else
            Console.WriteLine("No"); 
    } 
}
  
// This code is contributed by PrinciRaj1992


输出:
Yes