知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

  • 练习题

    • 选择题 & 填空题

    • 代码题

      • Boxes
      • The longest ascending sequence
      • Right Rotation
      • Book
      • Compact strings with AsciiCharSequence
      • Cinema
        • Topic
        • Problem
        • Hint & Explain
        • Solution
          • My solution
          • Other solution 1 (Pretty code!!!)
          • Other solution 2
          • Other solution 3
      • Calculate square
      • Count words
      • Robot control
      • Concat all strings without digits
      • Find the nearest number
      • Leaderboard
      • Split a list into sublists
      • Arithmetic average
      • Processing strings
  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • 练习题
  • 代码题
Jim
2022-07-06
目录

Cinema

# Topic

Multidimensional array

# Problem

Cinema

https://hyperskill.org/learn/step/1930

The cinema has n rows, each row consists of m seats (n and m do not exceed 20). A two-dimensional matrix stores the information on the sold tickets: the number 1 means that the ticket for this place is already sold, and the number 0 means that the place is available. You want to buy k tickets to neighboring seats in the same row. Find whether it can be done.

Input data format

On the input, the program gets the number of n rows and m seats. Then, there are n lines, each containing m numbers (0 or 1) separated by spaces. The last line contains the number k.

Output data format

The program should output the number of the row with k consecutive available seats. If there are several rows with k available seats, output the first row with these seats. If there is no such a row, output the number 0.

Sample Input 1:

3 4
0 1 0 1
1 1 0 1
1 0 0 1
2
1
2
3
4
5

Sample Output 1:

3
1

Sample Input 2:

3 3
0 1 0
1 0 0
1 1 1
3
1
2
3
4
5

Sample Output 2:

0
1

Sample Input 3:

2 4
1 1 0 0
0 0 1 1
4
1
2
3
4

Sample Output 3:

0
1

# Hint & Explain

If you're using a counter, remember to reset it when going from row to row.

# Solution

# My solution

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // put your code here
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();  // rows
        int m = scanner.nextInt();  // seats

        int[][] matrixOfSeats = new int[n][m]; // matrix

        for (int i = 0; i < matrixOfSeats.length; i++) {
            for (int j = 0; j < matrixOfSeats[i].length; j++) {
                matrixOfSeats[i][j] = scanner.nextInt();
            }
        }

        int k = scanner.nextInt(); // target
        int counter;
        int temp = 0;
        int targetOfRow = 0;
        boolean stop = false;

        for (int i = 0; i < matrixOfSeats.length; i++) {
            counter = 0;
            for (int j = 0; j < matrixOfSeats[i].length; j++) {
                if (matrixOfSeats[i][j] == 0) {
                    counter++;
                    temp = counter;

                    if (temp >= k) {
                        targetOfRow = i + 1;
                        stop = true;
                        break;
                    }

                } else {
                    counter = 0;
                }
            }
            if (stop) {
                break;
            }
        }

        System.out.println(targetOfRow);

    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

# Other solution 1 (Pretty code!!!)

import java.util.Scanner;

class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int m = scanner.nextInt();

        int[][] table = new int[n][m];

        int number;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                number = scanner.nextInt();
                table[i][j] = number;
            }
        }

        int k = scanner.nextInt();

        int counter;

        int i = 0;

        boolean available = false;

        LOOP:
        for (; i < n; i++) {
            counter = 0;
            for (int j = 0; j < m; j++) {
                if (table[i][j] == 0) {
                    counter++;
                } else {
                    counter = 0;
                }
                if (counter == k) {
                    available = true;
                    break LOOP;
                }
            }
        }

        if (available) {
            System.out.println(i + 1);
        } else {
            System.out.println(0);
        }

    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

# Other solution 2

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int row = sc.nextInt();
        int col = sc.nextInt();
        int[][] seats = new int[row][col];

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                seats[i][j] = sc.nextInt();
            }
        }

        int k = sc.nextInt();
        int count = 0;
        int maxCount = 0;

        for (int i = 0; i < row; i++) {
            count = 0;
            for (int j = 0; j < col; j++) {
                if (seats[i][j] == 0) {
                    count++;
                } else {
                    count = 0;
                }

                if (count > maxCount) {
                    maxCount = count;
                }
            }
            if (maxCount >= k) {
                System.out.println(i + 1);
                return;
            }
        }

        System.out.println(0);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

# Other solution 3

import java.util.Scanner;

class Main {

    public static String[] seats;
    public static int requiredTickets = 0;

    public static void main(String[] args) {
        // put your code here
        readInput();
        for (int i = 0; i < seats.length; i++) {
            if (seats[i].contains("0".repeat(requiredTickets))) {
                System.out.println(i + 1);
                return;
            }
        }
        System.out.println(0);
    }

    public static void readInput() {
        Scanner scanner = new Scanner(System.in);
        String buf = scanner.nextLine();
        String[] bufSplitted = buf.split(" ");
        int rows = Integer.parseInt(bufSplitted[0]);
        seats = new String[rows];
        for (int i = 0; i < rows; i++) {
            seats[i] = scanner.nextLine().replace(" ", "");
        }
        requiredTickets = scanner.nextInt();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
编辑 (opens new window)
#Java#Array#Problem
上次更新: 2022/09/25, 10:41:23
Compact strings with AsciiCharSequence
Calculate square

← Compact strings with AsciiCharSequence Calculate square→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式