求解一个JAVA程序题,好的话追加分!

2024-12-19 08:32:25
推荐回答(1个)
回答1:

import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class Test{

public static void main(String[] args){
HashSet range = new HashSet();

Scanner scanner = new Scanner(System.in);
//路长度
int length = scanner.nextInt();
//区间个数
int num = scanner.nextInt();
for (int i=0;i int start = scanner.nextInt();
int end = scanner.nextInt();
//区间集合
range.add(new T(start, end));
}
int count = 0;
for (int i=0;i if (!in(i, range)) count++;
}
System.out.println("rest " + count + " trees");
}
//如果>=起点并且<=终点则在范围内
public static boolean in(int object, HashSet range){
for (T t:range){
if (object>=t.start && object<=t.end) return true;
}
return false;
}

}

//区间类,只有两个成员开始和结束
class T{
public int start, end;
public T(int start, int end){
this.start = start;
this.end = end;
}
}