ZOJ-1121

41 篇文章 0 订阅

我去,这道模拟题的格式恶心死了,刚开始用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);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值