我正在开发一个飞机预订系统,飞机有 10 行。每排有从 A 到 K 的座位。第 5 个座位后有一个过道。
就我而言,1A、9C、10E 座位已经被预订了。
考虑一个人必须预订 3 个相邻的座位(不允许跨过道的座位)。
现在我需要归还可用的座位,例如 (1B,1C,1D & 1C,1D,1E & 1F,1G,1H & ...)
我如何从这个数组中获得连续的 3 个席位?
代码
公共(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++;
}
}
这将根据提供的参数获取每一行的连续座位。您需要做的是提供行,并配置输出的样子。 startWindow
和 endWindow
指的是长度为 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/