L1-043 阅览室(C&Java)PAT团体程序设计天梯赛-练习集

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

C 语言版本,提交无错误

#include <stdio.h>

int main()
{
    int N;
    scanf("%d", &N);

    while (N--) {
        int id;
        int books[1001][2] = {0};
        char key;
        int hh, mm; 

        while (scanf("%d %c %d:%d", &id, &key, &hh, &mm) && id) {

            // 如果是借 
            if (key == 'S') {
                books[id][0] = -1;              // 设置借的标志 
                books[id][1] = hh * 60 + mm;    // 计算时间,分钟 
            } else if (books[id][0] == -1) {
                // 是还书, 且之前有借的记录

                // 完整借书次数加 1 
                books[0][0]++;

                // 计算借书的时间 
                int total = hh * 60 + mm - books[id][1];

                // 加入总借阅时间 
                books[0][1] += total;       

                // 还原书的借阅标志,允许下次再借 
                books[id][0]++;

            } // if-else

        } // while id != 0

        int ave_m = 0;
        if (books[0][0] > 0) {
            double m = books[0][1] * 1.0 / books[0][0];
            // 四舍五入,小心入坑 
            ave_m = (int)(m + 0.5);
        }

        printf("%d %d\n", books[0][0], ave_m);

    } // while (N-- > 0)

    return 0;
 } 

Java语言版本,原理基本相同,提交有两个测试点过不去,知道的人还请给个提示

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();

        while (N-- > 0) {
            int id;
            // 存储借书记录(id,时间)
            int[][] books = new int[1001][2];

            while ((id = scanner.nextInt()) != 0) {
                char key = scanner.next().charAt(0);
                String[] time = scanner.next().split(":");
                int hh = Integer.parseInt(time[0]);
                int mm = Integer.parseInt(time[1]);

                // 如果是借书
                if (key == 'S') {
                    books[id][0] = -1;              // 设置借书标志
                    books[id][1] = hh * 60 + mm;    // 设置时间
                } else if (books[id][0] == -1){
                    // key == 'E'
                    // 如果是还书,且之前被借过,
                    // 则是一次有效的借和还记录,就计算时间。否则过滤

                    // 还书,使得可以再借
                    books[id][0]++;

                    // 计算借阅时间
                    int total = hh * 60 + mm - books[id][1];

                    // 今天的总借阅时间增加
                    books[0][1] += total;   

                    // 今天有效借阅次数加一
                    books[0][0]++;  


                } // if-else

            } // while id != 0

            int ave_m = 0;
            // 如果总借阅次数大于 0,避免除 0 错
            if (books[0][0] > 0) {
                double m = books[0][1] * 1.0 / books[0][0];
                // 四舍五入
                ave_m = (int)(m + 0.5);
            }

            System.out.println(books[0][0] + " " + ave_m);

        } // while (N-- > 0)

    } // main
}


猜你喜欢

转载自blog.csdn.net/qq_34732088/article/details/80509828