题解 | 符文激活

符文激活

https://www.nowcoder.com/practice/9aa964a14d2244c28909d2e3e7beb600

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int N = sc.nextInt();
        int[] nums=new int[N];
        for (int i = 0; i < N; i++) {
            nums[i]=sc.nextInt();
        }
        
        int shuijing=0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i]==1){
                boolean isActived=false;
                if (i>0&&nums[i-1]==-1){
                    isActived=true;
                }
                if (i<N-1&&nums[i+1]==-1){
                    isActived=true;
                }
                if (!isActived){
                    if (i<N-1&&nums[i+1]==0){
                        nums[i+1]=-1;
                        shuijing++;
                    }else if (i>0&&nums[i-1]==0){
                        nums[i-1]=-1;
                        shuijing++;
                    }else {
                        System.out.println(-1);
                        return;
                    }
                }
            }
        }
        System.out.println(shuijing);
    }
}

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务