天梯图书阅览室请你编写一个简单的图书借阅统计程序。当读者借书时,管理员输入书号并按下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
思路: 这道题目难度不大,就是模拟图书借阅过程,还书成功时,记录时间次数即可。我在做这道题目的时候是因为最后输出的时候,关于进位出错了。 我用的时第二行的输出,但是就是报错,使用第一行的就通过,如果哪位朋友可以解答以下,烦请指正。
//正确
cout << count << " " << (int)(1.0*sum/count + 0.5) << endl;
//错误
//cout << count << " " << ceil(1.0*sum/count) << endl;
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
struct Book{
char key = 'N';
int hh = 0;
int mm = 0;
}book[1005];
int calculate(int h1,int m1, int h2,int m2);
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i++){
int sum = 0;
int count = 0;
while(true){
int id;
char key,punctuation;
int h,m;
cin >> id >> key >> h >> punctuation >> m;
if(id == 0)break;
//还书时应注意情况
if(key =='E'){
if(book[id].key == 'E' || book[id].key=='N')continue;
}
if(key == 'E' && book[id].key == 'S'){
//还书成功,那么就计算时间啦
count++;
int time = calculate(book[id].hh,book[id].mm, h, m);
sum += time;
}
//更新书本信息
book[id].key = key;
book[id].hh = h;
book[id].mm = m;
}
if(count){
//正确
cout << count << " " << (int)(1.0*sum/count + 0.5) << endl;
//错误
//cout << count << " " << ceil(1.0*sum/count) << endl;
}else{
cout <<"0 0\n";
}
}
return 0;
}
int calculate(int h1,int m1, int h2,int m2){
return h2*60+m2 - h1*60-m1;
}