📜  计算需要的纸币数量

📅  最后修改于: 2021-10-26 02:32:46             🧑  作者: Mango

你有无限量的价值AB美元的钞票(A 不等于 B) 。您想使用恰好N 张纸币支付总共S美元。任务是找到您需要的价值美元的纸币数量。如果没有解决方案返回-1

例子:

方法:x是需要的有价票据的数量A则其余的票据即N – x必须为B的票据。因为,它们的总和是S,所以必须满足以下等式:

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
using namespace std;
 
// Function to return the amount of notes
// with value A required
int bankNotes(int A, int B, int S, int N)
{
    int numerator = S - (B * N);
    int denominator = A - B;
 
    // If possible
    if (numerator % denominator == 0)
        return (numerator / denominator);
    return -1;
}
 
// Driver code
int main()
{
    int A = 1, B = 2, S = 7, N = 5;
    cout << bankNotes(A, B, S, N) << endl;
}
     
// This code is contributed by mits


Java
// Java implementation of the approach
class GFG {
 
    // Function to return the amount of notes
    // with value A required
    static int bankNotes(int A, int B, int S, int N)
    {
        int numerator = S - (B * N);
        int denominator = A - B;
 
        // If possible
        if (numerator % denominator == 0)
            return (numerator / denominator);
        return -1;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int A = 1, B = 2, S = 7, N = 5;
        System.out.print(bankNotes(A, B, S, N));
    }
}


Python3
# Python3 implementation of the approach
 
# Function to return the amount of notes
# with value A required
def bankNotes(A, B, S, N):
 
    numerator = S - (B * N)
    denominator = A - B
 
    # If possible
    if (numerator % denominator == 0):
        return (numerator // denominator)
    return -1
 
# Driver code
A, B, S, N = 1, 2, 7, 5
print(bankNotes(A, B, S, N))
 
# This code is contributed
# by mohit kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the amount of notes
    // with value A required
    static int bankNotes(int A, int B,
                         int S, int N)
    {
        int numerator = S - (B * N);
        int denominator = A - B;
 
        // If possible
        if (numerator % denominator == 0)
            return (numerator / denominator);
        return -1;
    }
 
    // Driver code
    public static void Main()
    {
        int A = 1, B = 2, S = 7, N = 5;
        Console.Write(bankNotes(A, B, S, N));
    }
}
 
// This code is contributed by Akanksha Rai


PHP


Javascript


输出:
3