求解,dalao看看

P1047 [NOIP2005 普及组] 校门外的树

Qiyu111 @ 2023-04-19 18:08:21


by Qiyu111 @ 2023-04-19 18:12:18

@Qiyu111 大家看一下,能看到图片不?


by Vergil_Sparda @ 2023-04-19 18:17:38

为什么不直接发代码


by Vergil_Sparda @ 2023-04-19 18:24:28

@Qiyu111


by JYW2011 @ 2023-04-19 18:24:31

@Qiyu111


by Qiyu111 @ 2023-04-19 18:25:17

@qknb_QWQ ``` java package programming_exercises; import java.util.*; class Plant_trees { public static void main (String args[]){ Scanner sc = new Scanner(System.in); int a,b; a = sc.nextInt(); b = sc.nextInt(); int []start = new int[b]; int []end = new int[b]; for(int i = 0;i<b;i++){ start[i] = sc.nextInt(); end[i] = sc.nextInt(); } int []tree = new int[a+1]; for(int i=0;i < a+1;i++){ tree[i] = 1; } for(int i = 0;i < b;i++){ for(int j = start[i];j <= end[i];j++){ if(tree[j]==1){ tree[j] = 0; } } } int count = 0; for(int i = 0;i < a+1;i++){ if(tree[i]==1){ count++; } } System.out.println(count); } }


by Qiyu111 @ 2023-04-19 18:26:08

@qknb_QWQ 这样吗 本机样例能过,这里都不行


by Vergil_Sparda @ 2023-04-19 18:27:30

@Qiyu111 你再发一遍


by Qiyu111 @ 2023-04-21 22:47:54

@qknb_QWQ ``` java import java.util.*;

class Plant_trees {

public static void main (String args[]){
    Scanner sc = new Scanner(System.in);
    int a,b;
    a = sc.nextInt();
    b = sc.nextInt();
    int []start = new int[b];
    int []end = new int[b];
    for(int i = 0;i<b;i++){
        start[i] = sc.nextInt();
        end[i] = sc.nextInt();
    }
    int []tree = new int[a+1];
    for(int i=0;i < a+1;i++){
        tree[i] = 1;
    }
    for(int i = 0;i < b;i++){
        for(int j = start[i];j <= end[i];j++){
            if(tree[j]==1){
                tree[j] = 0;
            }
        }
    }
    int count = 0;
    for(int i = 0;i < a+1;i++){
        if(tree[i]==1){
            count++;
        }
    }
    System.out.println(count);
}

}


|