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;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Cymbals/article/details/81226819