查找无法获得计算机的客户数量的函数
编写一个函数“runCustomerSimulation”,它接受以下两个输入
a) 整数“n”:咖啡馆中的计算机总数和字符串:
b) 大写字母序列'seq':序列中的字母成对出现。第一次出现表示客户的到来;第二个表示同一客户的离开。
如果有一台未占用的计算机,将为客户提供服务。没有字母会出现超过两次。
不使用计算机离开的客户总是在当前使用计算机的客户之前离开。每个咖啡馆最多有 20 台电脑。
对于每组输入,该函数应输出一个数字,告诉有多少客户(如果有的话)在没有使用计算机的情况下离开了。如果所有客户都能使用计算机,则返回 0。
runCustomerSimulation (2, “ABBAJJKZKZ”) 应该返回 0
runCustomerSimulation (3, “GACCBDDBAGEE”) 应返回 1,因为“D”无法获取任何计算机
runCustomerSimulation (3, “GACCBGDDBAEE”) 应该返回 0
runCustomerSimulation (1, “ABCBCA”) 应返回 2,因为 'B' 和 'C' 无法获得任何计算机。
runCustomerSimulation(1, “ABCBCADEED”) 应该返回 3,因为 'B'、'C' 和 'E' 无法获得任何计算机。
资料来源:Fiberlink (maas360) 采访
我们强烈建议您最小化您的浏览器并首先自己尝试。
以下是查找无法获得任何计算机的客户数量的简单步骤。
1) 将结果初始化为 0。
2)遍历给定的序列。在遍历时,跟踪被占用的计算机(这可以通过跟踪只出现过一次并且出现时计算机可用的字符来完成)。在任何时候,如果占用计算机的数量等于“n”,并且有一个新客户,则将结果加 1。
重要的是要以一种可以指示客户是否拥有计算机的方式跟踪咖啡馆中的现有客户。请注意,在“ABCBCADEED”序列中,客户“B”没有获得座位,但仍然在咖啡馆,因为新客户“C”在序列中的下一个。
以下是上述想法的实现。
C++
// C++ program to find number of customers who couldn't get a resource.
#include
#include
using namespace std;
#define MAX_CHAR 26
// n is number of computers in cafe.
// 'seq' is given sequence of customer entry, exit events
int runCustomerSimulation(int n, const char *seq)
{
// seen[i] = 0, indicates that customer 'i' is not in cafe
// seen[1] = 1, indicates that customer 'i' is in cafe but
// computer is not assigned yet.
// seen[2] = 2, indicates that customer 'i' is in cafe and
// has occupied a computer.
char seen[MAX_CHAR] = {0};
// Initialize result which is number of customers who could
// not get any computer.
int res = 0;
int occupied = 0; // To keep track of occupied computers
// Traverse the input sequence
for (int i=0; seq[i]; i++)
{
// Find index of current character in seen[0...25]
int ind = seq[i] - 'A';
// If First occurrence of 'seq[i]'
if (seen[ind] == 0)
{
// set the current character as seen
seen[ind] = 1;
// If number of occupied computers is less than
// n, then assign a computer to new customer
if (occupied < n)
{
occupied++;
// Set the current character as occupying a computer
seen[ind] = 2;
}
// Else this customer cannot get a computer,
// increment result
else
res++;
}
// If this is second occurrence of 'seq[i]'
else
{
// Decrement occupied only if this customer
// was using a computer
if (seen[ind] == 2)
occupied--;
seen[ind] = 0;
}
}
return res;
}
// Driver program
int main()
{
cout << runCustomerSimulation(2, "ABBAJJKZKZ") << endl;
cout << runCustomerSimulation(3, "GACCBDDBAGEE") << endl;
cout << runCustomerSimulation(3, "GACCBGDDBAEE") << endl;
cout << runCustomerSimulation(1, "ABCBCA") << endl;
cout << runCustomerSimulation(1, "ABCBCADEED") << endl;
return 0;
}
Java
// JAVA program to find number of
//customers who couldn't get a resource.
class GFG
{
static int MAX_CHAR = 26;
// n is number of computers in cafe.
// 'seq' is given sequence of customer entry, exit events
static int runCustomerSimulation(int n, char []seq)
{
// seen[i] = 0, indicates that customer 'i' is not in cafe
// seen[1] = 1, indicates that customer 'i' is in cafe but
// computer is not assigned yet.
// seen[2] = 2, indicates that customer 'i' is in cafe and
// has occupied a computer.
char []seen = new char[MAX_CHAR];
// Initialize result which is number of customers who could
// not get any computer.
int res = 0;
int occupied = 0; // To keep track of occupied computers
// Traverse the input sequence
for (int i=0; i< seq.length; i++)
{
// Find index of current character in seen[0...25]
int ind = seq[i] - 'A';
// If First occurrence of 'seq[i]'
if (seen[ind] == 0)
{
// set the current character as seen
seen[ind] = 1;
// If number of occupied computers is less than
// n, then assign a computer to new customer
if (occupied < n)
{
occupied++;
// Set the current character as occupying a computer
seen[ind] = 2;
}
// Else this customer cannot get a computer,
// increment result
else
res++;
}
// If this is second occurrence of 'seq[i]'
else
{
// Decrement occupied only if this customer
// was using a computer
if (seen[ind] == 2)
occupied--;
seen[ind] = 0;
}
}
return res;
}
// Driver program
public static void main(String[] args)
{
System.out.println(runCustomerSimulation(2, "ABBAJJKZKZ".toCharArray()));
System.out.println(runCustomerSimulation(3, "GACCBDDBAGEE".toCharArray()));
System.out.println(runCustomerSimulation(3, "GACCBGDDBAEE".toCharArray()));
System.out.println(runCustomerSimulation(1, "ABCBCA".toCharArray()));
System.out.println(runCustomerSimulation(1, "ABCBCADEED".toCharArray()));
}
}
// This code is contributed by Princi Singh
Python3
# Python program function to find Number of customers who
# could not get a computer
MAX_CHAR = 26
# n is number of computers in cafe.
# 'seq' is given sequence of customer entry, exit events
def runCustomerSimulation(n, seq):
# seen[i] = 0, indicates that customer 'i' is not in cafe
# seen[1] = 1, indicates that customer 'i' is in cafe but
# computer is not assigned yet.
# seen[2] = 2, indicates that customer 'i' is in cafe and
# has occupied a computer.
seen = [0] * MAX_CHAR
# Initialize result which is number of customers who could
# not get any computer.
res = 0
occupied = 0 # To keep track of occupied
# Traverse the input sequence
for i in range(len(seq)):
# Find index of current character in seen[0...25]
ind = ord(seq[i]) - ord('A')
# If first occurrence of 'seq[i]'
if seen[ind] == 0:
# set the current character as seen
seen[ind] = 1
# If number of occupied computers is less than
# n, then assign a computer to new customer
if occupied < n:
occupied+=1
# Set the current character as occupying a computer
seen[ind] = 2
# Else this customer cannot get a computer,
# increment
else:
res+=1
# If this is second occurrence of 'seq[i]'
else:
# Decrement occupied only if this customer
# was using a computer
if seen[ind] == 2:
occupied-=1
seen[ind] = 0
return res
# Driver program
print (runCustomerSimulation(2, "ABBAJJKZKZ"))
print (runCustomerSimulation(3, "GACCBDDBAGEE"))
print (runCustomerSimulation(3, "GACCBGDDBAEE"))
print (runCustomerSimulation(1, "ABCBCA"))
print (runCustomerSimulation(1, "ABCBCADEED"))
# This code is contributed BHAVYA JAIN
C#
// C# program to find number of
// customers who couldn't get a resource.
using System;
class GFG
{
static int MAX_CHAR = 26;
// n is number of computers in cafe.
// 'seq' is given sequence of customer entry,
// exit events
static int runCustomerSimulation(int n, char []seq)
{
// seen[i] = 0, indicates that customer 'i' is not in cafe
// seen[1] = 1, indicates that customer 'i' is in cafe but
// computer is not assigned yet.
// seen[2] = 2, indicates that customer 'i' is in cafe and
// has occupied a computer.
char []seen = new char[MAX_CHAR];
// Initialize result which is number of customers
// who could not get any computer.
int res = 0;
int occupied = 0; // To keep track of occupied computers
// Traverse the input sequence
for (int i = 0; i < seq.Length; i++)
{
// Find index of current character in seen[0...25]
int ind = seq[i] - 'A';
// If First occurrence of 'seq[i]'
if (seen[ind] == 0)
{
// set the current character as seen
seen[ind] = (char)1;
// If number of occupied computers is less than
// n, then assign a computer to new customer
if (occupied < n)
{
occupied++;
// Set the current character as
// occupying a computer
seen[ind] = (char)2;
}
// Else this customer cannot get a computer,
// increment result
else
res++;
}
// If this is second occurrence of 'seq[i]'
else
{
// Decrement occupied only if this customer
// was using a computer
if (seen[ind] == 2)
occupied--;
seen[ind] = (char)0;
}
}
return res;
}
// Driver Code
public static void Main(String[] args)
{
Console.WriteLine(runCustomerSimulation(2,
"ABBAJJKZKZ".ToCharArray()));
Console.WriteLine(runCustomerSimulation(3,
"GACCBDDBAGEE".ToCharArray()));
Console.WriteLine(runCustomerSimulation(3,
"GACCBGDDBAEE".ToCharArray()));
Console.WriteLine(runCustomerSimulation(1,
"ABCBCA".ToCharArray()));
Console.WriteLine(runCustomerSimulation(1,
"ABCBCADEED".ToCharArray()));
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
0
1
0
2
3