题解 | 校门外的树

校门外的树

https://www.nowcoder.com/practice/0e8cfc82936048769af45967f3c4ef7e

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
            int L = in.nextInt();//设置路长
            int M = in.nextInt();//设置拔树的波次
            int arr[]=new int[L+1];
          
            for(int i=0;i<arr.length;i++){
                arr[i]=1; //种树
            }
            for(int j=0;j<M;j++){
                int start=in.nextInt();
                int end=in.nextInt(); //设置一波要拔的区域
                for(int i=start;i<=end;i++){ 
                    arr[i]=0;  //开始拔树
                }
            }
            int count=0;
            for(int i=0;i<arr.length;i++){
                if(arr[i]==1){ //还剩多少树
                    count+=1;
                }
            }
            System.out.println(count);
    }
}

全部评论

相关推荐

零零幺零零幺:至少再做一个项目,然后猛投小厂,不然有点难
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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