URAL - 2046 The First Day at School

本文介绍了一个用于生成大学课程表的算法案例,该算法能够根据学生选择的课程安排,生成一个清晰且易于阅读的周课程表。

Vasya is a young and very promising android. Today is his first day at University. Vasya has very carefully studied the list of all courses on the wall near the Dean’s office and has chosen the ones to attend. Now he wants to write down his own week timetable. Help him do this.
Input
The first line contains an integer n that is the number of courses Vasya is going to attend (1 ≤ n ≤ 12). After that the courses are listed, each is described in two lines.
The first line of a course description contains its name. The name of the course may consist of up to five words, which are divided by exactly one space (there are no spaces before the first word and after the last one). The words consist of capital and lowercase Latin letters. The length of every word is within the range from 1 to 10.
The second line of a course description contains the day of week and the number of a lesson, when it takes place. The day of week may take one of the three values: “Tuesday”, “Thursday” и “Saturday”. The number of a lesson is an integer from 1 to 4. There are no two courses, Vasya has chosen, taking place at the same time.
Output
Output the timetable as a table of the size 4×3. The columns of the table should correspond to the three academic days: the first column — to Tuesday, the second — to Thursday, the third — to Saturday. The rows should correspond to the four classes. The width of each column should be equal to 10 characters. The height of the row of the table equals to the height of the highest of its cells. If all the cells in the row are empty then the height of the row should be equal 1 character. If some word doesn’t find room in the current line, it should be placed in the next line. The text in the cell should be aligned to top and left borders. Make the table itself using characters “-” (ASCII 45), “+” (ASCII 43) and “|” (ASCII 124).
Example input
9
Physics
Thursday 3
Maths
Tuesday 1
Chemistry
Thursday 1
Physical education
Saturday 2
Astronomy
Saturday 4
Urban geography
Tuesday 4
History
Saturday 1
Modeling
Thursday 2
Biology
Thursday 4

这里写图片描述

模拟题,还不能叫大模拟,叫中模拟吧…..

生成如图那样的课程表。

一开始读错了题,以为是课程按空格换行,后来发现在按空格换行之前,需要考虑每个词的长度,所以将读取修改为了先按空格切割,然后再合并。临时改的,所以比较挫。

把混乱的输入排好序,然后一行一行的处理就可以了。

ac代码:

public class Main {
    public static void main(String[] args) throws IOException {
        InputReader reader = new InputReader();
        int n = reader.nextInt();
        Node[] nodes = new Node[n];
        int[] max = new int[5];

        for (int i = 0; i < n; i++) {
            nodes[i] = new Node();
            String[] in = reader.nextLine().split(" ");
            int cnt = in.length;
            int ii = 0;

            for (int j = 1; j < in.length;) {
                if(in[ii].length() + in[j].length() < 10) {
                    cnt--;
                    in[ii] += " " + in[j];
                    in[j] = "";
                    j++;
                    continue;
                }
                ii = j++;
            }

            nodes[i].cor = new String[cnt];
            ii = 0;

            for (int j = 0; j < in.length; j++) {
                if(in[j].equals("")) {
                    continue;
                }
                nodes[i].cor[ii++] = in[j];
            }



            String day = reader.next();
            nodes[i].rank = reader.nextInt();
            max[nodes[i].rank] = Math.max(nodes[i].cor.length, max[nodes[i].rank]);

            int id = 0;
            if (day.equals("Tuesday")) {
                id = 1;
            } else if (day.equals("Thursday")) {
                id = 2;
            } else {
                id = 3;
            }

            nodes[i].id = id;
        }
        Arrays.sort(nodes);

        System.out.println("+----------+----------+----------+");
        int index = 0;
        for (int i = 1; i <= 4; i++) {
            int st = index;
            int cur = 0;
            while (max[i]-- > 0) {
                index = st;
                System.out.print("|");
                for (int j = 1; j <= 3; j++) {
                    if (index >= n || nodes[index].id != j || nodes[index].rank != i) {
                        System.out.print("          |");
                        continue;
                    }
                    int len = 10;
                    if (nodes[index].cor.length > cur) {
                        len -= nodes[index].cor[cur].length();
                        System.out.print(nodes[index].cor[cur]);
                    }
                    for (int k = 0; k < len; k++) {
                        System.out.print(" ");
                    }
                    System.out.print("|");
                    index++;
                }
                cur++;
                System.out.println();
            }
            System.out.println("+----------+----------+----------+");
        }
    }

    static class Node implements Comparable<Node> {
        String[] cor;
        int id;
        int rank;

        @Override
        public int compareTo(Node o) {
            if (rank > o.rank) {
                return 1;
            }
            if (rank < o.rank) {
                return -1;
            }
            return id - o.id;
        }
    }
}
Java是一种具备卓越性能与广泛平台适应性的高级程序设计语言,最初由Sun Microsystems(现属Oracle公司)的James Gosling及其团队于1995年正式发布。该语言在设计上追求简洁性、稳定性、可移植性以及并发处理能力,同时具备动态执行特性。其核心特征与显著优点可归纳如下: **平台无关性**:遵循“一次编写,随处运行”的理念,Java编写的程序能够在多种操作系统与硬件环境中执行,无需针对不同平台进行修改。这一特性主要依赖于Java虚拟机(JVM)的实现,JVM作为程序与底层系统之间的中间层,负责解释并执行编译后的字节码。 **面向对象范式**:Java全面贯彻面向对象的设计原则,提供对封装、继承、多态等机制的完整支持。这种设计方式有助于构建结构清晰、模块独立的代码,提升软件的可维护性与扩展性。 **并发编程支持**:语言层面集成了多线程处理能力,允许开发者构建能够同时执行多项任务的应用程序。这一特性尤其适用于需要高并发处理的场景,例如服务器端软件、网络服务及大规模分布式系统。 **自动内存管理**:通过内置的垃圾回收机制,Java运行时环境能够自动识别并释放不再使用的对象所占用的内存空间。这不仅降低了开发者在内存管理方面的工作负担,也有效减少了因手动管理内存可能引发的内存泄漏问题。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值