赞
踩
在一个大型体育场内举办了一场大型活动,由于疫情防控的需要,要求每位观众的必须间隔至少一个空位才允许落座。现在给出一排观众座位分布图,座位中存在已落座的观众,请计算出,在不移动现有观众座位的情况下,最多还能坐下多少名观众。
输入描述:
一个数组,用来标识某一排座位中,每个座位是否已经坐人。0表示该座位没有坐人,1表示该座位已经坐人。
输出描述:
整数,在不移动现有观众座位的情况下,最多还能坐下多少名观众。
备注:
1<=数组长度<=10000
- import java.util.*;
-
- public class Main {
- public static void main(String[] args) {
- // int[] nums = new int[]{1, 0, 0, 0, 1};
- // 处理数据
- Scanner scanner = new Scanner(System.in);
- String string1 = scanner.next();
- int[] nums = new int[string1.length()];
- for (int i = 0; i < nums.length; i++) {
- nums[i] = string1.charAt(i) - '0';
- }
- if (nums.length == 1) {
- if (nums[0] == 0) {
- System.out.println(1);
- } else {
- System.out.println(0);
- }
- return;
- }
- int result = 0;
- for (int i = 0; i < nums.length; i++) {
- if (nums[i] == 0) {
- // 依次处理首位,中间,末尾,注意顺序
- if (i == 0 && nums[i + 1] == 0) {
- nums[i] = 1;
- result++;
- } else if (i > 0 && i < nums.length - 1 && nums[i + 1] == 0 && nums[i - 1] == 0) {
- nums[i] = 1;
- result++;
- } else if (i == nums.length - 1 && nums[i - 1] == 0) {
- nums[i] = 1;
- result++;
- }
- }
- }
- System.out.println(result);
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。