1642 - 游船出租
时间限制 : 1 秒
内存限制 : 32 MB
现有公园游船租赁处请你编写一个租船管理系统。当游客租船时,管理员输入船号并按下S键,系统开始计时;当游客还船时,管理员输入船号并按下E键,系统结束计时。船号为不超过100的正整数。当管理员将0作为船号输入时,表示一天租船工作结束,系统应输出当天的游客租船次数和平均租船时间。
题目输入
测试输入包含若干测试用例,每个测试用例为一整天的租船纪录,格式为:
每一天的纪录保证按时间递增的顺序给出。当读到船号为-1时,全部输入结束,相应的结果不要输出。
题目输出
对每个测试用例输出1行,即当天的游客租船次数和平均租船时间(以分钟为单位的精确到个位的整数时间)。
输入/输出样例
输入格式
1 S 07:10 2 S 09:35 1 E 10:00 2 E 12:50 0 S 18:00 0 S 07:00 -1
输出格式
2 183 0 0
C语言解答
#include <stdio.h> #include <string.h> typedef struct { int no; int h,m; int tag; }Node; Node arry[105]; int main() { int no,h,m; char op; int times,avetime; memset(arry,0,sizeof(arry)); times = 0 , avetime = 0; while( scanf( "%d" , &no ) && no != -1) { scanf(" %c %d:%d" , &op , &h , &m ); if( no == 0 ) //表示当前这组数据输入完毕,准备输出 { if( times == 0 ) printf( "0 0\n" ); //如果输入次数为0的时候 else { //四舍五入 if( avetime*1.0/times - avetime/times >= 0.5 ) avetime = avetime / times + 1; else avetime = avetime/times; printf( "%d %d\n" , times , avetime ); } //清零,准备记录下一组数据 memset(arry,0,sizeof(arry)); times = 0 , avetime = 0; } else { if( op == 'S' && arry[no].tag == 0 ) //开始op == 'S',并且以前没有开始过arry[no].tag == 0 { arry[no].h = h , arry[no].m = m; arry[no].tag = 1; } else if( op == 'E' && arry[no].tag == 1 ) //结束op == 'E',并且开始过arry[no].tag == 1 { int time = (h-arry[no].h)*60 - arry[no].m + m; avetime += time; times++; arry[no].tag = 0; } } } return 1; }
C++解答
#include <cstdio> #include <algorithm> using namespace std; int main() { while (true) { int k; scanf("%d", &k); if (-1 == k) break; char ss[10]; int h, m, a[105], cnt = 0; double sum = 0; scanf("%s %d:%d", ss, &h, &m); fill(a, a + 105, -1); while (k > 0) { int x = h * 60 + m; if (ss[0] == 'S') { if (a[k] == -1) a[k] = x; } else { if (a[k] != -1) { sum += x - a[k]; ++cnt; a[k] = -1; } } scanf("%d %s %d:%d", &k, ss, &h, &m); } if (0 == cnt) puts("0 0"); else printf("%d %.0f\n", cnt, sum / cnt + 1e-8); } return 0; }