AcWing 422. 校门外的树JAVA代码详解
原题链接
简单
作者:
光芒
,
2021-01-16 11:33:49
,
所有人可见
,
阅读 508
题目考点: 暴力(不是
题目思路: 先开一个大小为L+1的数组赋值为1 然后读入两边边界 把边界赋值为0 后面在统计剩下1的个数
相关题目:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int L = sc.nextInt();
int m = sc.nextInt();
int arr [] = new int[L+1];//包括0和L 所以多开1个
Arrays.fill(arr, 1);//将arr数组全部赋值为1
for(int i = 1;i<=m;i++) {
int l = sc.nextInt();//左边
int r = sc.nextInt();//右边
//把这个范围内赋值为0
for(int j=l;j<=r;j++)
arr[j] =0;
}
//计数
int ant = 0;
//统计为1 也就是树还剩多少
for(int i =0;i<=L;i++)
if(arr[i]==1) ant++;
System.out.println(ant);
}
}