排号機
描述:
實作一個簡易的銀行排号叫号系統
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
我的答案(java):
import java.util.Scanner;
import java.util.TreeSet;
public class Main{
private final String VIP="vip";
private long num=;// 号碼 1~100000
private TreeSet<Long> vipNums=new TreeSet<Long>();// 儲存vip号碼
private TreeSet<Long> queue=new TreeSet<Long>();// 隊列
private boolean quit;// 是否退出
public String[] input(Scanner sc){
String line=null;
String[] comd=null;
try{
line=sc.nextLine();
comd=line.split(" ");// 拆分指令
}catch(Exception e){
// e.printStackTrace();
comd=null;
printError();
}finally{
// sc.close();
}
return comd;
}
public void handle(String[] comd) throws Exception{
if(comd==null||comd.length==){
printError();
}
String str=comd[];// 提取指令
if(str.equals("get")){
if(num<||num>){// ERROR
printError();
}
if(comd.length==){
String str2=comd[];
if(str2.equals(VIP)){// get vip num
System.out.println(VIP+" "+num);
vipNums.add(num);
}else{
printError();
}
}else{
System.out.println(num);// get common num
}
queue.add(num);// 把取的号加入隊列
num++;
}else if(str.equals("call")){
Long first=null;
// vip first
if(vipNums.size()>){
first=vipNums.first();
System.out.println(VIP+" "+first);
vipNums.remove(first);
queue.remove(first);
}else if(queue.size()>){
first=queue.first();
System.out.println(first);
queue.remove(first);
}else{
printError();
}
}else if(str.equals("delete")){
if(comd.length==){
String str2=comd[];
Long numToDel=Long.parseLong(str2);
if(queue.contains(numToDel)){
if(vipNums.contains(numToDel)){
System.out.println(VIP+" "+numToDel);
vipNums.remove(numToDel);
}else{
System.out.println(numToDel);
}
if(queue.last().equals(numToDel)){
num--;
}
queue.remove(numToDel);
}else{
printError();
}
}else{
printError();
}
}else if(str.equals("count")){
System.out.println(queue.size());
}else if(str.equals("countN")){
if(comd.length==){
String str2=comd[];
Long numToCount=Long.parseLong(str2);
if(queue.contains(numToCount)){
int count=;
for(Long number : queue){
if(number<numToCount){
count++;
}
}
System.out.println(count);
}else{
printError();
}
}else{
printError();
}
}else if(str.equals("reset")){
num=;
vipNums.clear();
queue.clear();
}else if(str.equals("quit")){
quit=true;
}else{
printError();
}
}
public void printError(){
System.out.println("error");
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
Main m=new Main();
while(!m.quit){
String[] comd=m.input(sc);
try {
if(comd==null){
throw new Exception("comd is null");
}
m.handle(comd);
} catch (Exception e) {
m.printError();
// e.printStackTrace();
}
}
sc.close();
}
}