排号机 | |
描述: | 实现一个简易的银行排号叫号系统
get 取号 示例:"get"或"get vip" quit 退出排号机 示例:"quit" |
运行时间限制: | 无限制 |
内存限制: | 无限制 |
输入: | 每行只会有一条输入(比如:C语言可使用gets函数获取一行输入命令的字符串)。 |
输出: | 1)取号。可获取普通号和vip号码。如初始状态,输入"get",则获取普通号码,执行结果为"1",如再次输入"get vip",则获取VIP号码,执行结果为"vip 2"。如果末尾的2号被删除,则再次调用"get"时应输出"2"
7、退出排号机。例如输入"quit",则退出排号机,无需输出。 |
样例输入: | get get get get vip count countN 1 call quit |
样例输出: | 1 2 3 vip 4 4 1 vip 4 |
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class Main12 {
enum Cmd {get,call,delete,count,countN,reset,quit};
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin = new Scanner(System.in);
String line;
int count=1;
List<Integer> nums = new ArrayList<Integer>();
List<Integer> vipnums = new ArrayList<Integer>();
int cnum=0;
while (cin.hasNext()){
line = cin.nextLine();
String[] strs = line.split(" ");
String mycmd = strs[0];
switch (mycmd) {
case "get":
cnum=0;
if(count>100000){
System.out.println("error");
}else{
int c1=0,c2=0;
if(nums.size()>0)
c1 = Collections.max(nums);
if(vipnums.size()>0)
c2 = Collections.max(vipnums);
count = c1>c2?c1+1:c2+1;
if(strs.length==1){
System.out.println(count);
nums.add(count);
}else if(strs[1].equals("vip")){
System.out.println("vip "+count);
vipnums.add(count);
}
else
System.out.println("error");
}
break;
case "call":
cnum++;
if(cnum==6)
System.out.println("error");
int num;
if(vipnums.size()>0){
Collections.sort(vipnums);
num = vipnums.get(0);
System.out.println("vip "+num);
vipnums.remove(0);
}else if(nums.size()>0){
Collections.sort(nums);
num = nums.get(0);
System.out.println(num);
nums.remove(0);
}else
System.out.println("error");
break;
case "delete":
cnum=0;
int delnum;
if(strs.length!=2){
System.out.println("error");
}else{
try{
delnum = Integer.parseInt(strs[1]);
}catch (Exception e) {
System.out.println("error");
break;
}
if(nums.contains(delnum)){
System.out.println(delnum);
nums.remove(nums.indexOf(delnum));
}else if(vipnums.contains(delnum)){
System.out.println("vip "+delnum);
vipnums.remove(vipnums.indexOf(delnum));
}else{
System.out.println("error");
break;
}
}
break;
case "count":
cnum=0;
System.out.println(nums.size()+vipnums.size());
break;
case "countN":
cnum=0;
if(strs.length!=2){
System.out.println("error");
break;
}
int curnum;
try{
curnum = Integer.parseInt(strs[1]);
}catch (Exception e) {
System.out.println("error");
break;
}
if(nums.contains(curnum)){
Collections.sort(nums);
System.out.println(nums.indexOf(curnum)+vipnums.size());
}else if(vipnums.contains(curnum)){
Collections.sort(vipnums);
System.out.println(vipnums.indexOf(curnum));
}else{
System.out.println("error");
}
break;
case "reset":
cnum=0;
nums.clear();
vipnums.clear();
count=1;
break;
case "quit":
return;
default:
System.out.println("error");
break;
}
}
}
}