找座位-逻辑分析题

在这里插入图片描述
在这里插入图片描述

import java.util.Scanner;
public class Main{
    public static void main(String []args){
        Scanner in = new Scanner(System.in);

        String inString = in.nextLine();
        char[] temp = inString.toCharArray();
        int [] tempInt = new int[temp.length];
        int i = 0;
        for(char tempChar:temp){
            tempInt[i] = tempChar-'0';
            //System.out.print(tempInt[i]);
            i++;
        }
        // 记录能坐下的个数
        int count = 0;

        // 判断第一个元素的位置  是否能坐下
        if(tempInt[0] == 0){
            if(tempInt.length>1){
                if(tempInt[1] == 0){
                    count++;
                    tempInt[0] = 1;
                }
            }
            else{
                // length == 1 且 为空位 的条件下
                count++;
                tempInt[0]=1;
            }
        }

        // 掐头去尾 判断中间能否坐下
        for(int j=1;j<(tempInt.length-1);j++){
            if(tempInt[j] == 0 && tempInt[j-1] == 0 && tempInt[j+1] == 0){
                count++;
                tempInt[j]=1;
            }
        }

        // 判断末尾能否坐下
        if(tempInt.length>1 && tempInt[tempInt.length-2]==0 && tempInt[tempInt.length-1]==0){
            count++;
            tempInt[tempInt.length-1] = 1;
        }

        System.out.println(count);

    }
}
posted @ 2024-05-12 00:09  在天边偷看小天使  阅读(17)  评论(0)    收藏  举报  来源