【算法题】停车场车辆统计

241次阅读
没有评论

共计 967 个字符,预计需要花费 3 分钟才能阅读完成。

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 标题:停车场车辆统计 | 时间限制:1秒 | 内存限制:262144K | 语言限制:不限
 * 特定大小的停车场,数组cars[]表示,其中1表示有车,0表示没车。车辆大小不一,小车占一个车位(长度1),货车占两个车位(长度2),卡车占三个车位(长度3),统计停车场最少可以停多少辆车,返回具体的数目。
 * 输入描述:
 * 整型字符串数组cars[],其中1表示有车,0表示没车,数组长度小于1000。
 * 输出描述:
 * 整型数字字符串,表示最少停车数目。
 * 示例1
 * 输入
 * 1,0,1
 * 输出
 * 2
 * 说明 1个小车占第1个车位 第二个车位空 1个小车占第3个车位 最少有2辆车
 * 示例2
 * 输入
 * 1,1,0,0,1,1,1,0,1
 * 输出
 * 3
 * 说明 1个货车占第1、2个车位 第3、4个车位空 1个卡车占第5、6、7个车位 第8个车位空 1个小车占第9个车位 最少3辆车
 */
public class M_N_T_23 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();

        String[] strings = line.split(",");

        ArrayList<Integer> offCars = new ArrayList<>();
        offCars.add(-1);
        for (int i = 0; i < strings.length; i++) {
            if ("0".equals(strings[i])) {
                offCars.add(i);
            }
        }
        offCars.add(strings.length);

        int count = 0;
        for (int i = 0; i < offCars.size() - 1; i++) {
            int offset = offCars.get(i + 1) - offCars.get(i) - 1;
            int kache = offset / 3;
            int huoche = (offset - 3 * kache) / 2;
            int qiche = offset - 3 * kache - 2 * huoche;
            count += kache;
            count += huoche;
            count += qiche;
        }

        System.out.println(count);

    }
}
正文完
 1
裴先生
版权声明:本站原创文章,由 裴先生 2022-05-05发表,共计967字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
本站勉强运行: