zzuliOJ【土豪婷婷请吃饭】【解法:Java二维数组】

1、题目

题目官网: http://acm.zzuli.edu.cn/problem.php?id=2531

问题 C: 土豪婷婷请吃饭

时间限制: 2 Sec  内存限制: 256 MB
提交: 227  解决: 95
[状态] [讨论版] [提交] [命题人:zzuliacm]

题目描述

婷婷请ACM组的同学吃饭啦,婷婷包下了整家酒楼来款待ACM组的同学,同学们来了又走,走了又来,进进出出
请你计算什么时候人数最多。

输入

首先输入一个n,表示每个人的出入时间,1<=n<=5000
然后是n行,每行包括两个数字L和R,L表示同学的进场时间,R表示同学的出场时间,0<=L,R<=500
P.S.如果同学在a点出场,那么a点还是算在场,a+1点才不在场。

输出

输出一个数字,表示酒楼人数最多时的数量。

样例输入 Copy

10
7 9
4 9
7 8
2 5
3 4
5 6
1 6
2 8
3 5
3 4

5
3 6
5 6
1 8
0 6
7 8

5
4 12
9 18
6 7
0 2
5 7

样例输出 Copy

7
4
3

2、Java解法代码

我的思路:

  1. 创建二维数组,一行L与R的数据 对应 一行二维数组(按输入顺序,从上到下)。
  2. 最后,计算每一列的数据和,将最大值输出。
package B;

import java.util.Scanner;

public class _03C {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();

			int arr[][] = new int[n][503];
			for (int i = 0; i < n; i++) { // n行数据
				int left = sc.nextInt();
				int right = sc.nextInt();
				for (; left <= right; left++) {
					arr[i][left] = 1;
				}
			}
			// for (int a = 0; a < n; a++) { // 遍历行
			// for (int b = 0; b < 503; b++) { // 遍历列
			// System.out.print(arr[a][b] + " ");
			// }
			// System.out.println();
			// }
			int max = -1;
			for (int a = 0; a < 503; a++) { // 遍历列
				int sum = 0;
				for (int b = 0; b < n; b++) { // 遍历行
					sum += arr[b][a];
				}
				// System.out.println("sum :" + sum);
				max = max > sum ? max : sum;
			}
			System.out.println(max);
		}
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_44949135/article/details/108776893