天梯图书阅览室请你编写一个简单的图书借阅统计程序。当读者借书时,管理员输入书号并按下S
键,程序开始计时;当读者还书时,管理员输入书号并按下E
键,程序结束计时。书号为不超过1000的正整数。当管理员将0作为书号输入时,表示一天工作结束,你的程序应输出当天的读者借书次数和平均阅读时间。
注意:由于线路偶尔会有故障,可能出现不完整的纪录,即只有S
没有E
,或者只有E
没有S
的纪录,系统应能自动忽略这种无效纪录。另外,题目保证书号是书的唯一标识,同一本书在任何时间区间内只可能被一位读者借阅。
输入格式:
输入在第一行给出一个正整数NNN(≤10\le 10≤10),随后给出NNN天的纪录。每天的纪录由若干次借阅操作组成,每次操作占一行,格式为:
书号
([1, 1000]内的整数) 键值
(S
或E
) 发生时间
(hh:mm
,其中hh
是[0,23]内的整数,mm
是[0, 59]内整数)
每一天的纪录保证按时间递增的顺序给出。
输出格式:
对每天的纪录,在一行中输出当天的读者借书次数和平均阅读时间(以分钟为单位的精确到个位的整数时间)。
输入样例:
3
1 S 08:10
2 S 08:35
1 E 10:00
2 E 13:16
0 S 17:00
0 S 17:00
3 E 08:10
1 S 08:20
2 S 09:00
1 E 09:20
0 E 17:00
输出样例:
2 196
0 0
1 60
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
//** Class for buffered reading int and double values *//*
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
// ** call this method to initialize reader for InputStream *//*
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
// ** get next word *//*
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
// TODO add check for eof if necessary
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static boolean hasNext()throws IOException {
return tokenizer.hasMoreTokens();
}
static String nextLine() throws IOException{
return reader.readLine();
}
static char nextChar() throws IOException{
return next().charAt(0);
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static float nextFloat() throws IOException {
return Float.parseFloat(next());
}
}
public class Main {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
int n = Reader.nextInt();
for (int i = 0; i < n; i++) {
int[][]a = new int[1001][2];
double sum = 0;//时间和
int index = 0;//借书次数
while(true) {
int id = Reader.nextInt();
if (id==0) {
if (index!=0) {
System.out.print(index+" ");
System.out.println((int)(sum/index+0.5));
}else {
System.out.println(0+" "+0);
}
// id为0时后面的flag和时间就不管了
Reader.next();
Reader.next();
break;
}else {
char flag = Reader.nextChar();//S或者E
String time = Reader.next();
String[]temp = time.split(":");//小时和分钟
if (flag=='S') {
a[id][0] = Integer.parseInt(temp[0])*60+Integer.parseInt(temp[1]);
}else {
// 这本书已经被借了且没被还
if (a[id][1]==0&&a[id][0]!=0) {
a[id][1] = Integer.parseInt(temp[0])*60+Integer.parseInt(temp[1]);
sum+=(a[id][1]-a[id][0]);
index++;
// 书被借了可以被再次借
a[id][0] = 0;
a[id][1] = 0;
}else {
continue;
}
}
}
}
}
}
}
只对了一半,我考虑了一本书SSE和SEE的情况,SSE时用第二个S,因为缺失了一个E,第一个S就算无效记录;SEE用第一个E,同理缺失了一个S。还没找到问题,请发现问题的大佬下方评论