L1-043 阅览室 - java

题目解析

统计每天的借书次数和平均阅读时间

解题思路

用数组 t 去存储 这个书是否被借阅出去 并记录时间

当读到一个 S 时, 给 t 中对应id 附上时间

当读到一个 E 时, 判断 t 中对应 id 上有无数据
有就将当前的时间 - t 中的时间 加上时间 并 统计次数
没有就无需管这条数据

代码

import java.io.*;
import java.math.*;
import java.util.*;

public class Main
{
	static int N = (int) 1e3;

	static void solve()
	{
		int t[] = new int[N + 10];
		Arrays.fill(t, -1);

		int cnt = 0, times = 0;
		while (sc.hasNext())
		{
			int id = sc.nextInt();
			String op = sc.next();
			String time[] = sc.next().split(":");

			if (id == 0)
				break;

			int h = Integer.valueOf(time[0]);
			int m = Integer.valueOf(time[1]);

			int x = h * 60 + m;

			if (op.equals("S"))
				t[id] = x;
			else
			{
				if (t[id] == -1)
					continue;

				cnt++;
				times += x - t[id];
				t[id] = -1;
			}
		}

		if (cnt == 0)
			out.println(0 + " " + 0);
		else
			out.println(cnt + " " + (int) (times * 1.0 / cnt + 0.5));

	}

	public static void main(String[] args) throws FileNotFoundException, InterruptedException
	{
		int n = sc.nextInt();
		while (n-- > 0)
			solve();

		out.flush();
		out.close();
	}

	static InputStream inputStream = System.in;
	static InputReader sc = new InputReader(inputStream);
	static PrintWriter out = new PrintWriter(System.out);

	static class InputReader
	{
		public BufferedReader reader;
		public StringTokenizer tokenizer;

		public InputReader(InputStream stream)
		{
			reader = new BufferedReader(new InputStreamReader(stream), 32768);
			tokenizer = null;
		}

		public String next()
		{
			while (tokenizer == null || !tokenizer.hasMoreTokens())
			{
				try
				{
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e)
				{
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}

		boolean hasNext()
		{
			while (tokenizer == null || !tokenizer.hasMoreTokens())
			{
				try
				{
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (Exception e)
				{
					return false;
					// TODO: handle exception
				}
			}
			return true;
		}

		public int nextInt()
		{
			return Integer.parseInt(next());
		}

	}

}


团体程序设计天梯赛-练习集-java

赞赏