时间限制
400 ms
内存限制
64 MB
题目描述:
天梯图书阅览室请你编写一个简单的图书借阅统计程序。当读者借书时,管理员输入书号并按下S键,程序开始计时;当读者还书时,管理员输入书号并按下E键,程序结束计时。书号为不超过1000的正整数。当管理员将0作为书号输入时,表示一天工作结束,你的程序应输出当天的读者借书次数和平均阅读时间。
注意:由于线路偶尔会有故障,可能出现不完整的纪录,即只有S没有E,或者只有E没有S的纪录,系统应能自动忽略这种无效纪录。另外,题目保证书号是书的唯一标识,同一本书在任何时间区间内只可能被一位读者借阅。
输入格式:
输入在第一行给出一个正整数N(≤10),随后给出N天的纪录。每天的纪录由若干次借阅操作组成,每次操作占一行,格式为:
书号([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
统计每天的借书次数和平均阅读时间
emmmmmmm
用数组 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());
}
}
}
如果有说错的 或者 不懂的 尽管提 嘻嘻
一起进步!!!