club函数用来模拟一个俱乐部的顾客。
初始化情况下是0个顾客,俱乐部最大规模只能有50个顾客,当用户超过了最大规模,他们必须等在外面。当一些顾客离开了等待队列将减少。这个club函数将打印在俱乐部里面的顾客人数,和外面的等待人数。函数声明如下:void club(int x);正数x代表客人来了,负数x代表客人离开了俱乐部举例而言,club (40)打印40,0;接着club (20)打印50,10;接着club (-5)打印50,5;接着club (-30)打印25,0;接着club (-30)打印N/A;因为这是不可能实现的。请用c++编程实现club函数。为了确保函数工作正常,我们使用下列数据来测试函数是否正常,你认为该选哪个选项:
a 60
b 20 50 -10
c 40 -30
d 60 -5 -10 -10 10
e 10 -20
f 30 10 10 10 -60
g 10 10 10
h 10 -10 10
A a d e g
B c d f g
C a c d h
D b d g h
E c d e f
选E,代码如下:
#include <iostream>
const int capacity = 40;
void club( int x )
{
static int in = 0;
static int out = 0;
if( x >= 0 )
{
if( x >= ( capacity - in ) )
{
out = out +( x - ( capacity - in ) ) ;
in = capacity;
}
else
{
in = in + x;
}
}
else
{
if( in + x >= 0)
{
in = in + x;//in - ( -x )
}
else
{
cout << "N/A"<< endl;
return;
}
if( out <= ( capacity - in ) )
{
in = in + out;
out = 0;
}
else
{
in = capacity;
out = out - ( -x );
}
}
cout << in << " , "<< out << ";" << endl;
}