我在处理 jetbrains 学院任务时遇到问题



我应该这么做。

多维阵列影院

电影院有n排,每排有m个座位(n和m没有超过20(。二维矩阵存储已售出的票,1号表示这个地方的票已经售出已售出,数字0表示该位置可用。你想买k张同排相邻座位的票。查找是否可以做到。

输入数据格式

在输入中,程序获得n行和m个座位的数量。然后有n行,每行包含m个数字(0或1(,用空间。最后一行包含数字k。

输出数据格式

程序应输出k个连续行的编号可用座位。如果存在具有k个可用座位的若干排,输出这些座位的第一排。如果不存在这样的行,输出数字0。

但我一直在寻找一个合适的if语句来查找相同的相邻坐标k次

import java.util.Scanner;
import java.util.*;
class Main {
public static void main(String[] args) {
// put your code here
Scanner scanner= new Scanner(System.in);

int dim1=scanner.nextInt();
int dim2=scanner.nextInt();
int[][] twoDimArray=new int[dim1][dim2];

//  for (int i = 0; i < twoDimArray.length; i++) {
//   twoDimArray[i][i]=scanner.nextInt();
//System.out.println(Arrays.toString(twoDimArray[i]));
//      }

for (int i=0;i<dim1;i++){

for (int j=0;j<dim2;j++){
int current=scanner.nextInt();

twoDimArray[i][j]=current;



}
}



/*   for (int k = 0; k< dim1; k++) 
{
for (int l= 0; l< dim2;l++) 
{
System.out.print(twoDimArray[k][l] + " ");
}
System.out.println("");
}
*/
int seatsToBuy=scanner.nextInt();

for (int k = 0; k< dim1; k++) 
{
for (int l= 0; l< dim2;l++) 
{

if ((twoDimArray[k][l]==twoDimArray[k][l+1])&&l<dim2){
System.out.println(twoDimArray[k]);
break;
}
}
System.out.println("");
}

我对您的代码做了一些修改。你能试试下面修改过的代码吗?

import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int dim1 = scanner.nextInt();
int dim2 = scanner.nextInt();
int[][] twoDimArray = new int[dim1][dim2];
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
int current = scanner.nextInt();
twoDimArray[i][j] = current;
}
}
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
System.out.print(twoDimArray[i][j] + " ");
}
System.out.println();
}

int seatsToBuy=scanner.nextInt();
int minConsSeats = seatsToBuy - 1;
int rowNum = 0;
for (int k = 0; k< dim1; k++)
{
int count = 0;
for (int l= 0; l< dim2;l++) {
if (twoDimArray[k][l] == 0 && count == minConsSeats) {
count ++;
} else if (l+1 < dim2 && twoDimArray[k][l] == 0 && twoDimArray[k][l+1] == 0) {
count++;
}
}
if (count >= seatsToBuy) {
rowNum = k + 1;
break;
}
}
System.out.println("Available row number: "  + rowNum);
}
}

首先,我会用表达性名称命名变量,而不是I、j、k等。。

所以首先收集输入,我认为你做得很好,我只是重命名了变量:

Scanner scanner = new Scanner(System.in);
System.out.print("Number of Rows: ");
int nbRows = scanner.nextInt();
System.out.print("Number of Cols: ");
int nbCols = scanner.nextInt();
int[][] seats = new int[nbRows][nbCols];
for (int row = 0; row < nbRows; row++) {
System.out.println("Row " + row);
for (int col = 0; col < nbCols; col++) {
System.out.print("Col " + col +": ");
int availability = scanner.nextInt();
seats[row][col] = availability;
}
}
System.out.print("Number of Seats to buy: ");
int seatsToBuy = scanner.nextInt();

然后,对输入的内容进行一点健全性检查总是很有用的,所以这里有一个小片段来打印出调试的位置

for (int row = 0; row < nbRows; row++) {
for (int col = 0; col < nbCols; col++) {
System.out.print(seats[row][col] + " ");
}
System.out.println();
}

然后我将使用以下算法:在该行的开头启动计数器以跟踪相邻座位的最大数量(maxAdjacentSeatsInRow(。然后逐个座位,如果有,则递增计数器。如果不是,则将计数器重置为零。如果在任何时候计数器等于所需的座位数,那么那一排就是你想要返回的那一排:

int availableRow = -1;
for (int row = 0; row < nbRows; row++) {
int maxAdjacentSeatsInRow = 0;
for (int col = 0; col < nbCols; col++) {
if(seats[row][col] == 0)
maxAdjacentSeatsInRow++;
else
maxAdjacentSeatsInRow = 0;
if (maxAdjacentSeatsInRow == seatsToBuy) {
availableRow = row;
break; // break out of the col loop
}
}
if(availableRow >= 0) {
break; // break out of the row loop if we found a row
}
}
// print out result
if(availableRow == -1) {
System.out.println("No seats available");
} else {
System.out.println("Seats available at row: " + availableRow);
}

样本输出:

Number of Rows: 3
Number of Cols: 4
Row 0
Col 0: 1
Col 1: 1
Col 2: 1
Col 3: 1
Row 1
Col 0: 1
Col 1: 0
Col 2: 0
Col 3: 1
Row 2
Col 0: 1
Col 1: 1
Col 2: 0
Col 3: 0
Number of Seats to buy: 2
1 1 1 1 
1 0 0 1 
1 1 0 0 
Seats available at row: 1

答案中的行索引是从零开始的。也不要犹豫,让方法更容易阅读。这里有一个相同的版本,但有方法:

private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Number of Rows: ");
int nbRows = scanner.nextInt();
System.out.print("Number of Cols: ");
int nbCols = scanner.nextInt();
int[][] seats = gatherSeatsInputs(nbRows, nbCols);
System.out.print("Number of Seats to buy: ");
int seatsToBuy = scanner.nextInt();
printSeats(seats);
int availableRow = findAvailableRow(seats, seatsToBuy);
if(availableRow == -1) {
System.out.println("No seats available");
} else {
System.out.println("Seats available at row: " + availableRow);
}
}
private static int[][] gatherSeatsInputs(int nbRows, int nbCols) {
int[][] seats = new int[nbRows][nbCols];
for (int row = 0; row < nbRows; row++) {
System.out.println("Row " + row);
for (int col = 0; col < nbCols; col++) {
System.out.print("Col " + col +": ");
int current = scanner.nextInt();
seats[row][col] = current;
}
}
return seats;
}
private static int findAvailableRow(int[][] seats, int seatsToBuy) {
for (int rowIndex = 0; rowIndex < seats.length; rowIndex++) {
if(rowHasEnoughSeats(seats[rowIndex], seatsToBuy)) {
return rowIndex;
}
}
return -1;
}
private static boolean rowHasEnoughSeats(int[] row, int seatsToBuy) {
int maxAdjacentSeatsInRow = 0;
for (int seatAvailability : row) {
if (seatAvailability == 0) {
maxAdjacentSeatsInRow++;
if (maxAdjacentSeatsInRow == seatsToBuy) {
return true;
}
} else {
maxAdjacentSeatsInRow = 0;
}
}
return false;
}
private static void printSeats(int[][] seats) {
for (int[] row : seats) {
for (int seat : row) {
System.out.print(seat + " ");
}
System.out.println();
}
}

相关内容

最新更新