解题思路:运用数组来标识树的存在与否
注意事项:注意两端都有一棵树,实际树的数量是输入道路长度+1
参考代码:
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
/*
chi1 huo3 guo1*/
public class Main {
public static void main(String[] args)throws IOException{
BufferedReader bur = new BufferedReader(new InputStreamReader(System.in));
String[] str = bur.readLine().split(" ");
int num = Integer.parseInt(str[0]) + 1;
int n = Integer.parseInt(str[1]);
int[] tree = new int[num];
Arrays.fill(tree, 1);
for (int i = 0; i < n; i++) {
str = bur.readLine().split(" ");
for (int j = Integer.parseInt(str[0]); j <= Integer.parseInt(str[1]); j++) {
tree[j] = 0;
}
}
int sum = 0;
for (int i = 0; i < tree.length; i++) {
if(tree[i] == 1) {
sum++;
}
}
System.out.println(sum);
}//main
}//Class Main
0.0分
2 人评分