L1-043 阅览室 (20分)(已修改)

天梯图书阅览室请你编写一个简单的图书借阅统计程序。当读者借书时,管理员输入书号并按下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

不是满分,测试点二和四没通过,可以先看看思路,改好了在下面;

#include <iostream>
#include <string>
#include<algorithm>
#include<bits/stdc++.h>
#include<stack>
#include<set>
using namespace std;
struct book{
	int id;
	int  jie=0;
	int huan=0;
	int  time_jie;
	int  time_huan;
}a[1001];
int main() {
     int n;
     int count=0;
     int i,j;
     int time_sum=0;
     int jie_sum=0;
     cin>>n;
     while(n--){
     	jie_sum=0,time_sum=0;
     	while(1){
		 int hh,mm;
		 int id;
		 char x;
     	cin>>id>>x;
     	scanf("%d:%d",&hh,&mm);
     	if(id==0){
     		break;
		 }
     	else {
		   a[id].id=id;
     	if(x=='S'){
     		a[id].jie=1;
     		a[id].time_jie=hh*60+mm;
     		
		 }
		 else if(x=='E'){
		 	a[id].huan=1;
		 	a[id].time_huan=hh*60+mm;
		 }
		 }
		 if(a[id].jie==1&&a[id].huan==1){
		 	jie_sum++;
		 	time_sum+=a[id].time_huan-a[id].time_jie;
		 	a[id].jie=0;
		 	a[id].huan=0;
		 }
	 }
	 
	 cout<<jie_sum<<" ";
	 if(jie_sum==0){
	 	cout<<0;
	 }
	 else {
	 	float t=float(time_sum*1.0/jie_sum)-time_sum/jie_sum;
	 if(t>=0.5){
	 	cout<<time_sum/jie_sum+1;
	 }
	 else{
	 	cout<<time_sum/jie_sum;
	 }
	 }
	 cout<<endl;
	 for(i=1;i<1001;i++){
	 	a[i].id=i;
	 	a[i].jie=0;
	 	a[i].huan=0;
	 	a[i].time_huan=0;
	 	a[i].time_jie=0;
	 }
	 }
	 
}

这个是满分的,第二个测试点需要把信息全清空,
第四个测试点是最后输出的问题,

#include <iostream>
#include <string>
#include<algorithm>
#include<bits/stdc++.h>
#include<stack>
#include<set>
using namespace std;
typedef struct book{
	int id;
	char z=' ';
	int time=0;
}node;
int main(void){
	int n;
	cin>>n;
	
	while(n--){
		node a[1005];
		int count=0;
		int p[1005]={0};
		while(1){
			int i,j;
           	int id1,hh,mm;
	        char c ;
	     	cin>>id1>>c;
	    	scanf("%d:%d",&hh,&mm);
		    if(id1==0){
			break;
	  	}
		    int t=hh*60+mm;
		   if(c=='S'){
			a[id1].z='S';
			a[id1].time=t;
		}
	     	if(c=='E'&&a[id1].z=='S'){
			p[count]=t-a[id1].time;
			count++;
			a[id1].time=0;
			a[id1].z=' ';
		}
    	}
    	int j=0;
    	double sum=0;
    	for(j=0;j<count;j++){
    		sum+=p[j];
		}
		if(count){
			sum/=count;
		}
    	cout<<count<<" ";
    	printf("%.0f",sum);
		if(n){
			cout<<endl;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_努力努力再努力_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值