华为OJ-[排号机]

描述:
实现一个简易的银行排号叫号系统

get 取号 示例:”get”或”get vip”
call 叫号 示例:”call”
delete 删除号码 示例:”delete 5”
count 获取当前排队总人数 示例:”count”
countN 获取号码N以前的排队人数 示例:”countN”
reset 重置排号机 示例:”reset”
quit 退出排号机 示例:”quit”
运行时间限制: 无限制
内存限制: 无限制
输入:
每行只会有一条输入(比如:C语言可使用gets函数获取一行输入命令的字符串)。
1、若输入不符合要求(如:命令字非法,或其他认为输入的错误)均需输出”error”
2、每条输出后使用换行符隔开(如后面示例)
输出:
1)取号。可获取普通号和vip号码。如初始状态,输入”get”,则获取普通号码,执行结果为”1”,如再次输入”get vip”,则获取VIP号码,执行结果为”vip 2”。如果末尾的2号被删除,则再次调用”get”时应输出”2”
VIP号码有绝对的优先级。普通号和vip号码统一编号,取号均为连续号码。号码从1开始编号,最大为100000.

2)叫号。获取当前应该处理用户的号码。例如当前排队号码为1 2 3 4 5 7,当输入”call”,执行结果为”1”,如1为vip号码,则为”vip 1”.如果再连续调用6次,第六次执行结果应为”error”

3)删除号码。客户不想办理时可删除号码,叫号时则跳过此号码。例如当前排队号码为1 2 3 4 5,输入”delete 5”,执行结果为”5”,如果5为vip则显示”vip 5”。再次输出”delete 5”,执行结果为”error”

4)获取当前排队总人数。获取当前排队人数。例如当前排队号码为1 2 3 4 5 6,执行结果为”6”

5)获取在某个号码之前排队的总人数。例如当前排队号码为1 2 3 4 5 7,输入”countN 7”,执行结果为”5”

6、重置排号机。例如输入”reset”,则重置排号机,进入初始状态,无需输出。
7、退出排号机。例如输入”quit”,则退出排号机,无需输出。
样例输入:
get
get
get
get vip
count
countN 1
call
quit
样例输出:
1
2
3
vip 4
4
1
vip 4

C/C++源码:

//排号机
#include <iostream>
#include <string>
#include <list>
using namespace std;

list<int> Que, vip_Que;
int Num = 0;
int main()
{
    string input1, input2;
    int Flag = 0;

    void Get_vip();
    void Get();
    void Delete_Num(int tempNum);
    void Call();
    void Count();
    void CountN(int tempNum);
    void Reset_Num();

    while (getline(cin,input1))
    {

        if (input1 == "get")
        {
            ++Num;
            Get();
        }
        else if (input1 == "get vip")
        {
            ++Num;
            Get_vip();
        }
        else if (input1 == "call")
        {
            Call();//叫号函数
        }
        else if (input1 == "count")
        {
            Count();//统计总排号数函数
        }
        else if (input1 == "reset")
        {
            Reset_Num();//重置排号
        }
        else if (input1 == "quit")
        {
            break;
        }
        else
        {
            if (input1.find(' ') != string::npos)
            {
                input2 = input1.substr(input1.find(' ') + 1);
                input1 = input1.substr(0, input1.find(' '));
                if (input1 == "countN"||input1=="delete")
                {
                    int tempNum = 0;
                    for (int i = 0; i < input2.length(); ++i)
                    {
                        tempNum = tempNum * 10 + input2[i] - '0';
                    }
                    if (input1 == "countN")
                        CountN(tempNum);//统计tempNum之前的人数
                    else
                        Delete_Num(tempNum);
                }
                else
                    cout << "error\n";              
            }

            else
                cout << "error\n";
        }


    }
    system("PAUSE");
    return 0;
}

void Get_vip()
{
    cout << "vip " << Num << endl;
    vip_Que.push_back(Num);
}
void Get()
{
    cout << Num << endl;
    Que.push_back(Num);
}
void Call()
{
    if (!vip_Que.empty())
    {
        cout << "vip " << vip_Que.front() << endl;
        vip_Que.pop_front();
        --Num;
    }
    else if (!Que.empty())
    {
        cout << Que.front() << endl;
        Que.pop_front();
        --Num;
    }
    else
        cout << "error" << endl;
}
void Delete_Num(int tempNum)
{
    list<int>::iterator ite;
    int Flag = 2;//该号码是在vip中则为0,普通号为1;若均不存在,Flag为2,则输入error
    ite = vip_Que.begin();
    while (ite != vip_Que.end())
    {
        if (*ite == tempNum)
        {
            cout << "vip " << *ite << endl;
            ite = vip_Que.erase(ite);
            Flag = 0;
            break;
        }
        else
            ite++;
    }
    ite = Que.begin();
    while (Flag == 2 && ite != Que.end())
    {
        if (*ite == tempNum)
        {
            cout << *ite << endl;
            ite = Que.erase(ite);
            Flag = 1;
            break;
        }
        else
            ite++;
    }
    if (Flag == 2)
        cout << "error" << endl;
}
void Count()
{
    cout << Que.size() + vip_Que.size() << endl;
}
void CountN(int tempNum)
{
    list<int>::iterator ite;
    int Flag = 2;
    ite = vip_Que.begin();
    int coutN = 0;
    while (ite != vip_Que.end())
    {
        if (*ite == tempNum)
        {
            cout << coutN << endl;
            Flag = 0;
            break;
        }
        else
            ite++;
        ++coutN;
    }
    ite = Que.begin();
    coutN = 0;
    while (Flag == 2 && ite != Que.end())
    {
        if (*ite == tempNum)
        {
            cout << vip_Que.size() + coutN << endl;
            ite = Que.erase(ite);
            Flag = 1;
            break;
        }
        else
            ite++;
        ++coutN;
    }
    if (Flag == 2)
        cout << "error" << endl;

}
void Reset_Num()
{
    Num = 0;
    Que.clear();
    vip_Que.clear();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值