Subject to reproduce
Ladder reading room invite you to write a simple book lending statistics. When a reader library, enter the ISBN administrator and press the S key, the program start time; when the reader has the book, ISBN administrator input and press the E key, the program ends timing. ISBN no more than 1,000 positive integer. When the administrator ISBN 0 as input to indicate end of the day, your program should output the number of library readers and average reading time of the day.
Note: Due to occasional line fault, incomplete record may appear that not only S E, or E not only record S, the system should be able to automatically ignore this invalid record. In addition, to ensure that the title ISBN is a unique identifier of the book, the same book could only have been a reader to borrow at any time interval.
Input formats:
A first input line of a given positive integer N (≤10), followed by N days given record. Daily record of the number of times the operation consisting of borrowing, one row for each operation, in the format:
ISBN ([1, 1000] is an integer) key (S or E) occurrence time (hh: mm, where hh is an integer of [0,23] in, mm is [0, 59] within an integer)
Each day record of guarantee given in chronological order of increasing.
Output formats:
To record daily output of that day in a row 读者借书次数和平均阅读时间
(in minutes to the nearest integer bit of time).
Sample input:
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
Sample output:
2 196
0 0
1 60
answer
This question is doing is a failure, at first glance very simple, the failure of two things
1. accustomed to c ++, forget scanf, scanf input with this question will be much better.
2. This question needs to be noted that if a book is borrowed, it is also too, can still be borrowed.
100 ++ AND
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct node
{
int id;
char key;
string time;
bool isVaild;//该节点的数据是否有效
};
//字符串转数字
int string_to_integer(string str)
{
stringstream ss(str);
int num;
ss>>num;
return num;
}
//计算时间
int caculateTime(string str1,string str2)
{
int hour=string_to_integer(str1.substr(0,2))-string_to_integer(str2.substr(0,2));
int minute=string_to_integer(str1.substr(3,2))-string_to_integer(str2.substr(3,2));
return hour*60+minute;
}
node nds[1005];
//初始化结构体数组
void init()
{
for(int i=0;i<1005;i++)
{
nds[i].isVaild=false;
}
}
int main()
{
node nd;
int n;
cin>>n;
for(int i=0; i<n; i++)
{
init();
double timeSum=0;
int cnt=0;
while(true)
{
cin>>nd.id>>nd.key>>nd.time;
//如果输入的id是0,则代表该日已完成,需打印
if(nd.id==0)
{
if(cnt==0)
{
cout<<0<<" "<<0<<endl;
}
else
{
cout<<cnt<<" "<<int(timeSum/cnt+0.5)<<endl;
}
break;
}
//如果是借的
if(nd.key=='S')
{
nds[nd.id].id=nd.id;
nds[nd.id].key=nd.key;
nds[nd.id].time=nd.time;
nds[nd.id].isVaild=true;
}
//如果是还的
else
{
if(nds[nd.id].isVaild)
{
timeSum+=caculateTime(nd.time,nds[nd.id].time);
cnt++;
nds[nd.id].isVaild=false;
}
}
}
}
return 0;
}