ACM-XZNU-1008 校门外的树 java 解题报告
1008:校门外的树- 查看
- 提交
- 统计
- 提问
- 时间限制:?
- 1000ms?
- 内存限制:?
- 65536kB
- 描述
- 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米。我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置;数轴上的每个整数点,即0,1,2,……,L,都种有一棵树。
马路上有一些区域要用来建地铁,这些区域用它们在数轴上的起始点和终止点表示。已知任一区域的起始点和终止点的坐标都是整数,区域之间可能有重合的部分。现在要把这些区域中的树(包括区域端点处的两棵树)移走。你的任务是计算将这些树都移走后,马路上还有多少棵树。
- 输入
- 输入的第一行有两个整数L(1 <= L <= 10000)和 M(1 <= M <= 100),L代表马路的长度,M代表区域的数目,L和M之间用一个空格隔开。接下来的M行每行包含两个不同的整数,用一个空格隔开,表示一个区域的起始点和终止点的坐标。
- 输出
- 输出包括一行,这一行只包含一个整数,表示马路上剩余的树的数目。
- 样例输入
500 3150 300100 200470 471
- 样例输出
298import java.util.Scanner;
public class Main {public static void main(String[] args) {int L,n;int count = 0;Scanner sc = new Scanner(System.in); L = sc.nextInt(); n = sc.nextInt(); Boolean tree[] = new Boolean[L+1]; Integer space[][] = new Integer[n][2]; for (int k = 0; k < tree.length; k++) {tree[k] = true;} for (int i = 0; i < n; i++) {space[i][0] = sc.nextInt();space[i][1] = sc.nextInt();} for (int i = 0; i < n; i++) {int x = space[i][0];int y = space[i][1];for (int j = x; j <=y; j++) {tree[j] = false;}} for (int i = 0; i < tree.length; i++) { if(tree[i]==true)count++;} System.out.println(count);}}
?这一题主要是利用一个boolean数组实现的很简单
马路上有一些区域要用来建地铁,这些区域用它们在数轴上的起始点和终止点表示。已知任一区域的起始点和终止点的坐标都是整数,区域之间可能有重合的部分。现在要把这些区域中的树(包括区域端点处的两棵树)移走。你的任务是计算将这些树都移走后,马路上还有多少棵树。
500 3150 300100 200470 471
298import java.util.Scanner;
public class Main {public static void main(String[] args) {int L,n;int count = 0;Scanner sc = new Scanner(System.in); L = sc.nextInt(); n = sc.nextInt(); Boolean tree[] = new Boolean[L+1]; Integer space[][] = new Integer[n][2]; for (int k = 0; k < tree.length; k++) {tree[k] = true;} for (int i = 0; i < n; i++) {space[i][0] = sc.nextInt();space[i][1] = sc.nextInt();} for (int i = 0; i < n; i++) {int x = space[i][0];int y = space[i][1];for (int j = x; j <=y; j++) {tree[j] = false;}} for (int i = 0; i < tree.length; i++) { if(tree[i]==true)count++;} System.out.println(count);}}?这一题主要是利用一个boolean数组实现的很简单