需要设计多个航班之间是否有时间重叠或交集,如果有重叠或交集则给航班打上一个1的标识,没有则设为0的标识,写出Java示例代码,需考虑增删改3种情况
时间: 2023-06-27 07:07:02 浏览: 71
项目2_1751022_李翠琪1
假设每个航班有起飞时间和降落时间两个属性,可以定义一个Flight类来表示航班,示例代码如下:
```java
import java.util.ArrayList;
import java.util.List;
public class Flight {
private String id;
private long departureTime;
private long arrivalTime;
private int overlap;
public Flight(String id, long departureTime, long arrivalTime) {
this.id = id;
this.departureTime = departureTime;
this.arrivalTime = arrivalTime;
this.overlap = 0;
}
public String getId() {
return id;
}
public long getDepartureTime() {
return departureTime;
}
public void setDepartureTime(long departureTime) {
this.departureTime = departureTime;
}
public long getArrivalTime() {
return arrivalTime;
}
public void setArrivalTime(long arrivalTime) {
this.arrivalTime = arrivalTime;
}
public int getOverlap() {
return overlap;
}
public void setOverlap(int overlap) {
this.overlap = overlap;
}
public boolean overlapsWith(Flight other) {
return !(this.arrivalTime <= other.departureTime || other.arrivalTime <= this.departureTime);
}
public static List<Flight> findOverlappingFlights(List<Flight> flights) {
List<Flight> overlappingFlights = new ArrayList<>();
for (int i = 0; i < flights.size(); i++) {
Flight flight1 = flights.get(i);
for (int j = i + 1; j < flights.size(); j++) {
Flight flight2 = flights.get(j);
if (flight1.overlapsWith(flight2)) {
flight1.setOverlap(1);
flight2.setOverlap(1);
overlappingFlights.add(flight1);
overlappingFlights.add(flight2);
}
}
}
return overlappingFlights;
}
}
```
上面的代码中,Flight类有四个属性:id表示航班的编号,departureTime表示起飞时间,arrivalTime表示降落时间,overlap表示是否有重叠,初始值为0。Flight类还有一个overlapsWith方法,用于判断两个航班是否有重叠,如果有则返回true,否则返回false。Flight类还有一个静态方法findOverlappingFlights,输入一个航班列表,返回有重叠的航班列表。这个方法使用两个嵌套的for循环遍历所有的航班对,如果两个航班有重叠,则将它们的overlap属性设为1,并将它们加入到overlappingFlights列表中。
下面是一个示例程序,演示了如何使用Flight类来处理航班重叠问题:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class FlightApp {
public static void main(String[] args) {
List<Flight> flights = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入航班信息:");
System.out.print("编号:");
String id = scanner.next();
if (id.equals("exit")) {
break;
}
System.out.print("起飞时间:");
long departureTime = scanner.nextLong();
System.out.print("降落时间:");
long arrivalTime = scanner.nextLong();
flights.add(new Flight(id, departureTime, arrivalTime));
List<Flight> overlappingFlights = Flight.findOverlappingFlights(flights);
System.out.println("航班列表:");
for (Flight flight : flights) {
System.out.println(flight.getId() + "\t" + flight.getDepartureTime() + "\t" + flight.getArrivalTime() + "\t" + flight.getOverlap());
}
System.out.println("重叠航班列表:");
for (Flight overlappingFlight : overlappingFlights) {
System.out.println(overlappingFlight.getId() + "\t" + overlappingFlight.getDepartureTime() + "\t" + overlappingFlight.getArrivalTime() + "\t" + overlappingFlight.getOverlap());
}
}
}
}
```
上面的示例程序中,通过Scanner从控制台输入航班信息,每输入一个航班就调用Flight.findOverlappingFlights方法查找有重叠的航班,并输出整个航班列表和重叠航班列表。程序会一直运行,直到输入exit为止。
阅读全文