求解,dalao看看

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

@[Qiyu111](/user/783615) 大家看一下,能看到图片不?
by Qiyu111 @ 2023-04-19 18:12:18


为什么不直接发代码
by Vergil_Sparda @ 2023-04-19 18:17:38


@[Qiyu111](/user/783615)
by Vergil_Sparda @ 2023-04-19 18:24:28


@[Qiyu111](/user/783615)
by JYW2011 @ 2023-04-19 18:24:31


@[qknb_QWQ](/user/469492) ``` 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:25:17


@[qknb_QWQ](/user/469492) 这样吗 本机样例能过,这里都不行
by Qiyu111 @ 2023-04-19 18:26:08


@[Qiyu111](/user/783615) 你再发一遍
by Vergil_Sparda @ 2023-04-19 18:27:30


@[qknb_QWQ](/user/469492) ``` 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); } } ```
by Qiyu111 @ 2023-04-21 22:47:54


|