L1-043 Reading room (structure + rounding)

Description

L1-043 Reading Room (20 minutes) Tianti Book Reading Room, please write a simple statistical program for book borrowing. When a reader borrows a book, 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 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 a day's work, and your program should output the number of borrowed books and the average reading time of the day.

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

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

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

The records of each day are guaranteed to be given in increasing order of time.

Output format:
For each day's record, output the number of readers' borrowing times and average reading time of the day in one line (in minutes, the integer time accurate to the single digit).

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 sample:
2 196
0 0
1 60

answer

This question needs to pay attention to the two situations of borrowing or not repaying and borrowing or not being ignored. A value can be used to record the status of the book, 1 means borrowed, 0 means not borrowed, and can be updated at any time. The final time shall be rounded up.

Rounding method

C++
call function

 double a;
    cout<<ceil(a)<<endl;   //向上取整
    cout<<floor(a)<<endl;   //向下取整
    cout<<round(a)<<endl;   //四舍五入

C
directly uses int to cast

double a;
    cout<<(int)a<<endl;//向下取整
    cout<<(a>(int)a?(int)a+1:(int)a)<<endl;//向上取整
    cout<<(int)(a+0.5)<<endl;//四舍五入

AC code

#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct node
{
    
    
    int f;
    int h,m;
} a[1100];
int main()
{
    
    
    int n;
    cin>>n;
    while(n--)
    {
    
    
        for(int i=0; i<1100; i++)
            a[i].f=0;
        int num,hh,mm;
        char c;
        int time=0,peo=0;
        while(~scanf("%d %c %02d:%02d",&num,&c,&hh,&mm))
        {
    
    
            if(num==0)break;
            if(c=='S')
            {
    
    
                a[num].f=1;
                a[num].h=hh;
                a[num].m=mm;
            }
            else if(c=='E')
            {
    
    
                if(a[num].f==1)
                {
    
    
                    a[num].f=0;
                    if(hh==a[num].h)
                    {
    
    
                        time+=mm-a[num].m;
                    }
                    else
                    {
    
    
                        time+=(hh-a[num].h-1)*60;
                        time+=mm+60-a[num].m;
                    }
                     peo++;
                }
            }
        }
        if(peo==0)
        cout<<0<<" "<<0<<endl;
      else
      {
    
    
         int t=round(time*1.0/peo*1.0);
            cout<<peo<<" "<<t<<endl;
      }

    }
    return 0;
}

Guess you like

Origin blog.csdn.net/rookie636/article/details/109714554