华为上机题-排号机
1 问题描述
实现一个简易的银行排号叫号系统
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
2 我的解答
package hhu.marthevin.daily;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main5 {
public static void main(String[] args) throws Exception{
Scanner scanner = new Scanner(System.in);
Queue<String> queue = new LinkedList<String>();
//System.out.println(checkCommand("countN 1"));
while(true)
{
String command = scanner.nextLine();
//System.out.println(command);
if(!checkCommand(command))
{
//System.out.println("ss");
System.out.println("error");
continue;
}
if(command.equals("get vip"))
{
//System.out.println("sss");
if(queue.size()<=100000)
{
int size = queue.size();
size++;
queue.add("vip "+size);
System.out.println("vip "+size);
}else{
System.out.println("error");
}
}
else if(command.equals("get"))
{
//System.out.println("kkk");
if(queue.size()<=100000)
{
int size = queue.size();
size++;
queue.add(size+"");
System.out.println(queue.size());
}else{
System.out.println("error");
continue;
}
}else if(command.contains("call")){
if(queue.size()>0)
{
boolean flag = true;
for(String ele:queue)
{
if(ele.contains("vip"))
{
System.out.println(ele);
queue.remove(ele);
flag = false;
break;
}
}
if(flag){
System.out.println(queue.remove());
}
}else{
System.out.println("error");
}
}else if(command.contains("delete")){
if(queue.size()==0)
{
System.out.println("error");
continue;
}
String[] arr = command.split(" ");
int id = Integer.parseInt(arr[1]);
//System.out.println(id);
boolean flag = true;
for(String ele:queue)
{
if(ele.contains(" "))
{
//System.out.println("c");
int tmp = Integer.parseInt(ele.split(" ")[1]);
if(id == tmp){
queue.remove(ele);
System.out.println(ele);
flag = false;
break;
}
}else{
//System.out.println("n");
int tmp = Integer.parseInt(ele);
if(id==tmp)
{
queue.remove(ele);
System.out.println(ele);
flag = false;
break;
}
}
}
if(flag)
{
System.out.println("error");
}
}else if(command.equals("count"))
{
if(queue.isEmpty())
{
System.out.println(0+"");
continue;
}
System.out.println(queue.size());
}else if(command.contains("countN"))
{
int id = Integer.parseInt(command.split(" ")[1]);
if(id>queue.size())
{
//System.out.println("ss");
System.out.println("error");
continue;
}
int index = 1;
for(String ele:queue)
{
if(ele.contains(" "))
{
int tmp = Integer.parseInt(ele.split(" ")[1]);
if(tmp==id)
{
break;
}
}else{
int tmp = Integer.parseInt(ele);
if(tmp==id)
{
break;
}
}
index++;
}
if(index>queue.size())
{
//System.out.println("sss");
System.out.println("error");
}else{
System.out.println(index);
}
}else if(command.contains("reset")){
queue.removeAll(queue);
}else if(command.contains("quit"))
{
break;
}
}
}
public static boolean checkCommand(String s)
{
boolean flag = false;
String reg = "get|get\\svip|delete\\s\\d+|count|countN\\s\\d+|reset|quit|call";
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(s);
if(matcher.matches())
{
flag = true;
}
return flag;
}
}
3 阅读须知
- 本文代码仅供参考,不喜勿喷
- 有何问题,可以在评论中指出
- 任何copy本案例,产生严重后果,本人概不负责