给定一个由N个整数组成的数组,其中A i表示第i个人拥有的纸币货币。可能的货币是5、10和20。所有N个人正排在队列中,等待从X购买价格为5卢比的冰淇淋。最初,X的初始余额为0。检查X是否能够为正在等待购买冰淇淋的每个人提供零钱。
例子:
Input:a[] = {5, 5, 5, 10, 20}
Output: YES
When the fourth person chance comes to buy an ice-cream, X has three Rs 5
change, hence X gives him 1, and now when the fifth person
comes to buy the ice-cream, X has two Rs 5 and one Rs 10 note, hence he
gives him one Rs 10 and one Rs 5 note.
Input: a[] = {5, 10, 10, 20}
Output: NO
方法是跟踪5卢比和10卢比的货币数量。不会使用20卢比的货币,因为它是一个人可以提供的最高货币,因此不能将其作为找零。初始化两个变量以计数Rs 5(fiveCount)和Rs 10(tenCount)。如果此人的货币为10卢比,并且FiveCount> 0,则减少FiveCount并增加tenCount。如果X没有5卢比,则X无法给此人所需的零钱。如果此人的面额为5美元,则将五点数加1。如果此人的收入为20卢比,则以下三个条件为:
- 如果FiveCount> 0和tencount> 0,请同时减少两者。
- 否则,ifiveCount> = 3,则将Fivecount减三。
- 否则,返回false。
如果队列中的所有人都得到了更改,则打印“是”,否则打印“否”。
以下是上述想法的实现。
C++
// C++ program to check whether X can give change
// to every person in the Queue
#include
using namespace std;
// Function to check if every person will
// get the change from X
int isChangeable(int notes[], int n)
{
// To count the 5$ and 10& notes
int fiveCount = 0;
int tenCount = 0;
// Serve the customer in order
for (int i = 0; i < n; i++) {
// Increase the number of 5$ note by one
if (notes[i] == 5)
fiveCount++;
else if (notes[i] == 10) {
// decrease the number of note 5$ and
// increase 10$ note by one
if (fiveCount > 0) {
fiveCount--;
tenCount++;
}
else
return 0;
}
else {
// decrease 5$ and 10$ note by one
if (fiveCount > 0 && tenCount > 0) {
fiveCount--;
tenCount--;
}
// decrease 5$ note by three
else if (fiveCount >= 3) {
fiveCount -= 3;
}
else
return 0;
}
}
return 1;
}
// Driver Code
int main()
{
// queue of customers with available notes.
int a[] = { 5, 5, 5, 10, 20 };
int n = sizeof(a) / sizeof(a[0]);
// Calling function
if (isChangeable(a, n))
cout << "YES";
else
cout << "NO";
return 0;
}
Java
// Java program to check
// whether X can give
// change to every person
// in the Queue
import java.io.*;
class GFG
{
// Function to check if
// every person will
// get the change from X
static int isChangeable(int notes[],
int n)
{
// To count the 5$
// and 10& notes
int fiveCount = 0;
int tenCount = 0;
// Serve the customer
// in order
for (int i = 0; i < n; i++)
{
// Increase the number
// of 5$ note by one
if (notes[i] == 5)
fiveCount++;
else if (notes[i] == 10)
{
// decrease the number
// of note 5$ and
// increase 10$ note by one
if (fiveCount > 0)
{
fiveCount--;
tenCount++;
}
else
return 0;
}
else
{
// decrease 5$ and
// 10$ note by one
if (fiveCount > 0 &&
tenCount > 0)
{
fiveCount--;
tenCount--;
}
// decrease 5$
// note by three
else if (fiveCount >= 3)
{
fiveCount -= 3;
}
else
return 0;
}
}
return 1;
}
// Driver Code
public static void main (String[] args)
{
// queue of customers
// with available notes.
int a[] = {5, 5, 5, 10, 20};
int n = a.length;
// Calling function
if (isChangeable(a, n) > 0)
System.out.print("YES");
else
System.out.print("NO");
}
}
// This code is contributed
// by anuj_67.
Python3
# Python program to check whether X can
# give change to every person in the Queue
# Function to check if every person
# will get the change from X
def isChangeable(notes, n):
# To count the 5$ and 10& notes
fiveCount = 0
tenCount = 0
# Serve the customer in order
for i in range(n):
# Increase the number of 5$ note by one
if (notes[i] == 5):
fiveCount += 1
elif(notes[i] == 10):
# decrease the number of note 5$
# and increase 10$ note by one
if (fiveCount > 0):
fiveCount -= 1
tenCount += 1
else:
return 0
else:
# decrease 5$ and 10$ note by one
if (fiveCount > 0 and tenCount > 0):
fiveCount -= 1
tenCount -= 1
# decrease 5$ note by three
elif (fiveCount >= 3):
fiveCount -= 3
else:
return 0
return 1
# Driver Code
# queue of customers with available notes.
a = [5, 5, 5, 10, 20 ]
n = len(a)
# Calling function
if (isChangeable(a, n)):
print("YES")
else:
print("NO")
# This code is contributed by PrinciRaj1992
C#
// C# program to check
// whether X can give
// change to every person
// in the Queue
using System;
class GFG
{
// Function to check if
// every person will
// get the change from X
static int isChangeable(int []notes,
int n)
{
// To count the 5$
// and 10& notes
int fiveCount = 0;
int tenCount = 0;
// Serve the customer
// in order
for (int i = 0; i < n; i++)
{
// Increase the number
// of 5$ note by one
if (notes[i] == 5)
fiveCount++;
else if (notes[i] == 10)
{
// decrease the number
// of note 5$ and
// increase 10$ note by one
if (fiveCount > 0)
{
fiveCount--;
tenCount++;
}
else
return 0;
}
else
{
// decrease 5$ and
// 10$ note by one
if (fiveCount > 0 &&
tenCount > 0)
{
fiveCount--;
tenCount--;
}
// decrease 5$
// note by three
else if (fiveCount >= 3)
{
fiveCount -= 3;
}
else
return 0;
}
}
return 1;
}
// Driver Code
public static void Main ()
{
// queue of customers
// with available notes.
int []a = {5, 5, 5, 10, 20};
int n = a.Length;
// Calling function
if (isChangeable(a, n) > 0)
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
}
// This code is contributed
// by anuj_67.
YES