L1-043 reading room (20 points)

Keywords: C++

TIANTI book reading room, please write a simple book borrowing statistics program. When readers borrow books, the administrator enters the book number and presses the S key, and the program starts timing; When the reader returns the book, the administrator enters the book number and presses the E key, and the program ends the timing. The book number is a positive integer not exceeding 1000. When the administrator enters 0 as the book number, it means the end of the day. Your program should output the readers' borrowing times and average reading time of the day.

Note: due to the occasional fault of the line, incomplete records may appear, that is, only S without E, or only E without S. The system shall be able to automatically ignore this invalid record. In addition, the title guarantee book number is the unique identification of the book. The same book can only be borrowed by one reader at any time.

Input format:

Input gives a positive integer n (≤ 10) on the first line, and then gives the record of N days. The daily record consists of several borrowing operations. Each operation occupies one line. The format is:

Book number ([1, 1000] integer) key value (S or E) occurrence time (hh:mm, where hh is an integer in [0,23], and MM is an integer in [0,59])

The records of each day shall be given in the order of increasing time.

Output format:

For the daily record, the readers' borrowing times and average reading time of the day (integer time accurate to bits in minutes) are output in one line.

Input example:

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

Output example:

2 196
0 0
1 60

answer

for the first time

#include<iostream>
#include<cmath>
using namespace std;

int main()
{	
	int booknumber[1001][2]={0};//Borrowing time for books (hours, minutes)
	char c[1001];
	int n;
	cin>>n;
	int end[1001][2]={0};
	int end0=0;//Number of people 
	int end1=0;//time 
	for(int i=0;i<n;)
	{
		int num;
		char type;
		int hour;
		int mintue;
		char a;
		cin>>num>>type>>hour>>a>>mintue;
		if(num==0)
		{
			end[i][0]=end0;
			end[i][1]=end1;
			end0=0;
			end1=0;
			i++;
			continue;
		}
		if(type-'E'==0 && booknumber[num][0]!=0)//When you return it 
		{
			end0++;
			end1=end1+(hour-booknumber[num][0])*60;
			if(mintue>=booknumber[num][1]) 
				end1=end1+(mintue-booknumber[num][1]);	
			else
				end1=end1-(booknumber[num][1]-mintue);
			booknumber[num][0]=0;
			booknumber[num][1]=0;
		}
		if(type-'S'==0 && booknumber[num][0]==0)//When borrowing 
		{
			booknumber[num][0]=	hour;
			booknumber[num][1]=	mintue;
		} 
	}
	for(int i=0;i<n;i++)
	{
		cout<<end[i][0]<<' ';
		if(end[i][0]!=0 && end[i][1]!=0 )
		{
			float a=end[i][1]/(end[i][0]*1.0);
			cout<<round(a)<<endl;
		}
		else
			cout<<end[i][1]<<endl;
	}
    return 0;
}


First of all, I found on the Internet that I didn't consider borrowing all the time. After removing "booknumber[num][0]==0", the test points 2 and 3 still failed
Secondly, there are many codes. For example, when inputting, you can directly convert h+m into the form of only M

The second time

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,i,m,t=0,hh,mm,map[1001]={0},min[1001]={0};
	double sum=0;
	char c,d;
	cin>>n;i=n;
	while(i>0)
	{
		cin>>m>>c>>hh>>d>>mm;
		if(m==0)
		{
			cout<<t<<" ";			
			if(t!=0)
				printf("%.0f\n",sum/t);
			else
				cout<<0<<endl;		
			i--;
			sum=0;
			memset(map,0,sizeof(map));
			t=0;	
		}
		else if(c=='S')
		{
			map[m]=1;
			min[m]=hh*60+mm;
		}
		else if(c=='E'&&map[m]==1)
		{
			map[m]=0;
			sum+=(hh*60+mm-min[m]);			
			t++;
		}	
	}
	
} 

third time

In the process of Baidu, it is found that structures can also be used

#include<bits/stdc++.h>
using namespace std;

struct book {
	bool lend = false;//Be lent out
	int outTime;//How long has it been on loan
};
struct day {
	int counter = 0;//Correct lending times
	int time = 0;//Total lending time of all books on this day
};
int main()
{
	int n;
	cin >> n;
	day *d = new day[n]; //Create days array
	for (int i = 0; i < n; i++) {
		book b[1001];	//Create an array of the status of the record book and recreate it every day to achieve automatic recovery
		while (true) {	
			int name, hour, min; char c,maohao;
			cin >> name >> c >> hour >> maohao >> min;
			if (name == 0)break;
			if (c == 'S') {
				b[name].outTime = hour * 60 + min;//The time is converted into minutes, which makes the calculation simple
				b[name].lend = true;
			}
			else {
				if (b[name].lend) {//This book will only be recorded if it has been lent out
					d[i].time+=(hour * 60 + min - b[name].outTime);
					d[i].counter++;
					b[name].lend = false;
					b[name].outTime = 0;
				}
			}
		}
	}
	for (int i = 0; i < n; i++) {
		cout << d[i].counter << ' ';
		if (d[i].counter) {
			cout << round((double)d[i].time / (double)d[i].counter) << endl;
		}
		else {
			cout << 0 << endl;
		}
	}
	return 0;
}


Refer to:

https://blog.csdn.net/BCadillac/article/details/104129670

Posted by kellydigital on Sun, 05 Dec 2021 06:17:36 -0800