华为- 排号机(OJ通过)

题目很简单,用c++的vector,list等均可

 

#include <iostream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;

struct node{
	int num;
	bool vip;
};

list<struct node> lst;
int cur_num;

void get_num(bool vip)
{
	struct node nd;
	nd.num = cur_num;
	nd.vip = vip;
	if(!vip){
		lst.push_back(nd);
		cout<<nd.num<<endl;
	}
	else{
		list<struct node>::iterator it=lst.begin();
		while(it != lst.end()){
			if( !(*it).vip)
				break;
			it++;
		}
		lst.insert(it,nd);
		cout<<"vip "<<nd.num<<endl;
	}
	cur_num++;
}

void call_num()
{
	if(lst.empty()){
		cout<<"error"<<endl;
		return;
	}
	struct node nd = lst.front();
	if(nd.vip)
		cout<<"vip "<<nd.num<<endl;
	else
		cout<<nd.num<<endl;
	// 末尾号处理?
	lst.erase(lst.begin());
}

void delete_num(int num)
{
	list<struct node>::iterator it=lst.begin();
	while(it != lst.end()){
		if((*it).num == num){
			if((*it).vip)
				cout<<"vip "<<num<<endl;
			else
				cout<<num<<endl;
			lst.erase(it);
			if(num == cur_num-1)
				cur_num--;
			return;
		}
		it++;
	}
	cout<<"error"<<endl;
}
void count_num()
{
	cout<<lst.size()<<endl;
}

void countN_num(int num)
{
	struct node nd;
	list<struct node>::iterator it=lst.begin();
	int count = 0;
	while(it != lst.end()){
		nd = *it;
		if(nd.num == num){
			cout<<count<<endl;
			return;
		}
		count++;
		it++;
	}
	cout<<"error"<<endl;
}


int main()
{
	string cmd;
	lst.resize(0);
	cur_num = 1;
	while(getline(cin,cmd)){
		if(cmd == "get")
			get_num(false);
		else if(cmd == "get vip")
			get_num(true);
		else if(cmd == "call")
			call_num();
		else if(cmd.find("delete")!=std::string::npos){
			string temp = cmd.substr(cmd.find(' ')+1);
			delete_num(atoi(temp.c_str()));
		}
		else if(cmd == "count")
			count_num();
		else if(cmd.find("countN")!=std::string::npos){
			string temp = cmd.substr(cmd.find(' ')+1);
			countN_num(atoi(temp.c_str()));
		}
		else if(cmd == "reset"){
			lst.resize(0);
			cur_num = 1;
		}
		else if(cmd == "quit")
			break;
	}
	return 0;
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值