华为- 排号机(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;
}

点赞