L1-043. 阅览室

解题思路:

a.由于借出或归还信息可能丢失,是故如果第一次就见到归还直接跳过(借出信息丢失),连续两次见到同一本书的借出第一次借出信息丢掉(第一次归还信息丢失)。

b.计算平均阅读时间,有除零错误,浮点数四舍五入(加上0.5f强制转换为int)

 

 

 

#include <iostream>
#include <map>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct borrowBooks
{
int frequency;
double time;
};
struct lendBooks
{
char ch;
int hour;
int minute;
};
int main()
{
int n;
cin >> n;
struct borrowBooks* checkOut[n];
map<int, struct lendBooks*> library;
map<int, struct lendBooks*>::iterator iter;
int bookId=1;
int hour, minute;
char operation;
for(int i=0; i<n; i++)
{
struct borrowBooks* temp = (struct borrowBooks*)malloc(sizeof(struct borrowBooks));
temp->frequency = 0;
temp->time = 0;
checkOut[i] = temp;
cin >> bookId;
cin >> operation;
scanf("%d:%d", &hour, &minute);
while(bookId!=0)
{


if((iter=library.find(bookId))==library.end())
{
if(operation=='S') //没有找到,只有借书记录才会插入

{
struct lendBooks* books = (struct lendBooks*)malloc(sizeof(struct lendBooks));
books->ch = operation;
books->hour = hour;
books->minute = minute;
library[bookId] = books;
}
}
else
{
if(operation=='E') //找到了,且操作不同,第二次为还
{
hour = (hour - iter->second->hour) * 60 + (minute - iter->second->minute);
//cout << "diyicifenzhong" << hour << endl;
checkOut[i]->time = checkOut[i]->time + hour;
checkOut[i]->frequency++;
library.erase(iter);
}
else{
iter->second->ch = operation;
iter->second->hour = hour;
iter->second->minute = minute;
}
}
cin >> bookId;
cin >> operation;
scanf("%d:%d", &hour, &minute);
}
for(iter = library.begin(); iter!=library.end(); iter++)
free(iter->second);
library.clear();

}

for(int i=0; i<n; i++)
{
cout << checkOut[i]->frequency << " ";
if(checkOut[i]->frequency!=0)
{
int t;
checkOut[i]->time = checkOut[i]->time/checkOut[i]->frequency;
t = checkOut[i]->time + 0.5f;
cout << t << endl;
}
else
cout << 0 << endl;
}

return 0;
}

posted @ 2017-08-10 14:57  diamondDemand  阅读(832)  评论(0编辑  收藏  举报