我去,这道模拟题的格式恶心死了,刚开始用C++搞的,都快写完了发现输入格式太恶心,有些后面有空格要处理,有些没有。。没有trim函数非常不爽,只能换JAVA重新写了一遍。。还是想吐槽这题的输入和输出都非常坑。。
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;
public class Main
{
static Map<String, Integer> map = new HashMap<String, Integer>();
static LinkedList<String> queue = new LinkedList<String>();
static int n;
public static void main(String[] args)
{
System.out.println("Program 2 by team X");
Scanner sc = new Scanner(System.in);
int now = 0;
n = sc.nextInt();
sc.nextLine();
while (sc.hasNextLine())
{
String s = sc.nextLine().trim();
if (s.equals("PRINT"))
print(now);
else
{
String action = s.substring(0, 9).trim();
String book = null;
if (action.equals("ADD") || action.equals("RETURN"))
{
if (action.equals("ADD"))
{
book = s.substring(9, 39).trim();
int d = Integer.valueOf(s.substring(39));
map.put(book, d);
}
else
book = s.substring(9).trim();
queue.addFirst(book);
now += map.get(book);
while (now > n)
{
String back = queue.removeLast();
now -= map.get(back);
}
}
else
{
book = s.substring(9).trim();
for (int i = 0; i < queue.size(); i++)
if (queue.get(i).equals(book))
{
queue.remove(i);
now -= map.get(book);
break;
}
}
}
}
System.out.println("End of program 2 by team X");
sc.close();
}
static void print(int space)
{
for (int i = 0; i < queue.size(); i++)
System.out
.format("%-29s%5d\n", queue.get(i), map.get(queue.get(i)));
System.out.format("%-29s%5d\n\n", "AVAILABLE SHELF SPACE:", n - space);
}
}