java - 从座位预订系统中获取连续 3 个值?

标签 java arrays algorithm multidimensional-array

我正在开发一个飞机预订系统,飞机有 10 行。每排有从 A 到 K 的座位。第 5 个座位​​后有一个过道。

就我而言,1A、9C、10E 座位已经被预订了。

考虑一个人必须预订 3 个相邻的座位(不允许跨过道的座位)。

现在我需要归还可用的座位,例如 (1B,1C,1D & 1C,1D,1E & 1F,1G,1H & ...)

我如何从这个数组中获得连续的 3 个席位?

enter image description here

代码

公共(public)类飞机{

public static void main(String[] args) {
    System.out.println("Welcome to the seat reservation system!");
    char[][] seats = new char [10][11];
    ArrayList<String> reservedSeats = new ArrayList<>();
    for (int i=0;i<10;i++){
        seats[i][0] = 'A';
        seats[i][1] = 'B';
        seats[i][2] = 'C';
        seats[i][3] = 'D';
        seats[i][4] = 'E';
        seats[i][5] = 'F';
        seats[i][6] = 'G';
        seats[i][7] = 'H';
        seats[i][8] = 'I';
        seats[i][9] = 'J';
        seats[i][10] = 'K';
    }
    Scanner console = new Scanner(System.in);
    int filled = 0;
    printSeats(seats);
    System.out.println("Enter seat (e.g. 1A) or zero to quit the program.");//How to make 0 the exit key?
    String input = console.nextLine();
    while ((filled <48) &&(input.length() >0)) {
        int row = input.charAt(0) - '1';
        int col = input.charAt(1) - 'A';
        if (row<0 || row>11 || col<0 || col>10) {
            System.out.println("Input error. Enter seat to assign (e.g., '1A'), " +
                    "or zero to quit.");
            input = console.nextLine();
        } else {
            if (seats[row][col] != 'X') {
                seats[row][col] = 'X';
                filled++;
                System.out.println();
                printSeats(seats);
            }

            if (filled < 48) {
                System.out.println("Enter seat to assign (e.g., '1A'), " +
                        "or zero to quit:");
                input = console.nextLine();
            }
        }
    }
    System.out.println("Final seat assignments: ");
    printSeats(seats);
}

private static void printSeats(char[][] seats) {

    for (int i = 0; i < seats.length; i++) {
        System.out.println((i + 1) + "   " +
                seats[i][0] + seats[i][1] + seats[i][2] + seats[i][3] + seats[i][4] + "   " +
                seats[i][5] + seats[i][6] + seats[i][7]+ seats[i][8] + seats[i][9]+ seats[i][10]);
    }

    System.out.println("There are XX number of seats available.");

    getConsecutiveSeats(seats, 5, 3);
}

/**
 * @param row The row of seats considered
 * @param sectionLength the length of each section split by aisles
 * @param numConsecutive the number of consecutive seats to consider
 */
public static void getConsecutiveSeats(char[][] row, int sectionLength, int numConsecutive) {
    int endWindow = numConsecutive;
    for (int startWindow = 0; endWindow <= row.length; startWindow++) {
        char[][] consecutiveSeats = Arrays.copyOfRange(row, startWindow, endWindow);
        boolean validConsecutiveSeats = (startWindow >= sectionLength && endWindow >= sectionLength) ||
                (startWindow <= sectionLength && endWindow <= sectionLength);
        if (!Arrays.toString(consecutiveSeats).contains("X") && validConsecutiveSeats) {
            System.out.println(consecutiveSeats);
        }
        endWindow++;
    }
}

最佳答案

考虑这个方法:

   /**
     * @param row The row of seats considered
     * @param sectionLength the length of each section split by aisles
     * @param numConsecutive the number of consecutive seats to consider
     */
    public static void getConsecutiveSeats(char[] row, int sectionLength, int numConsecutive) {
        int endWindow = numConsecutive;
        for (int startWindow = 0; endWindow <= row.length; startWindow++) {
            char[] consecutiveSeats = Arrays.copyOfRange(row, startWindow, endWindow);
            boolean validConsecutiveSeats = (startWindow >= sectionLength && endWindow >= sectionLength) ||
                    (startWindow <= sectionLength && endWindow <= sectionLength);
            if (!Arrays.toString(consecutiveSeats).contains("X") && validConsecutiveSeats) {
                System.out.println(consecutiveSeats);
            }
            endWindow++;
        }
    }

这将根据提供的参数获取每一行的连续座位。您需要做的是提供行,并配置输出的样子。 startWindowendWindow 指的是长度为 numConsecutive 的滑动窗口,这是你考虑的一排座位数(例如,你说 3).

这样测试:

public static void main(String[] args) {
     char[] row = {'X', 'B', 'C', 'D', 'E', 'A', 'X', 'C', 'D', 'E'};
     getConsecutiveSeats(row, 5, 3);
}

哪些输出:

BCD
CDE
CDE

提供变量 row 是正确的。

关于java - 从座位预订系统中获取连续 3 个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51384952/

相关文章:

java - RxJava : "java.lang.IllegalStateException: Only one subscriber allowed!"

java - 在 ExecutorService 上调用 shutdown() 的原因

java - Android studio 从外部模块继承类

javascript - ajax post 整数和字符串数组

algorithm - 如何证明树上顶点覆盖的贪心算法的正确性?

java - 如何将类名传递给抽象父类(super class)构造函数?

java - 使用 split 时,字符串数组中的第一个元素为空

c - 将二维数组中的数组存储在 C 中

android - Android中AES加解密算法

php - 如何从 php 中的平面结构有效地构建树?