import java.util.Scanner; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.ArrayList; import java.util.stream.Collectors; class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int numberOfWineshops = scanner.nextInt(); int numberOfPickedWineshops = scanner.nextInt(); int referencePrice = scanner.nextInt(); int[] WineshopPrices = new int[numberOfWineshops]; for (int i = 0; i < numberOfWineshops; i++) { WineshopPrices[i] = scanner.nextInt(); } Arrays.sort(WineshopPrices); int[][] priceDifference = new int[numberOfWineshops][2]; for (int i = 0; i < numberOfWineshops; i++) { int price = WineshopPrices[i]; priceDifference[i][0] = price; priceDifference[i][1] = Math.abs(price - referencePrice); } List<int[]> sortedPriceDifference = Arrays.stream(priceDifference) .sorted(Comparator.comparingInt(Wineshop -> Wineshop[1])) .collect(Collectors.toList()); List<Integer> pickedWineshopPrices = new ArrayList<>(); for (int i = 0; i < numberOfPickedWineshops; i++) { pickedWineshopPrices.add(sortedPriceDifference.get(i)[0]); } pickedWineshopPrices.sort(Integer::compareTo); for (int i = 0; i < pickedWineshopPrices.size(); i++) { System.out.print(pickedWineshopPrices.get(i)); if (i != pickedWineshopPrices.size() - 1) { System.out.print(" "); } } } };这段代码每行注释下
时间: 2024-01-30 17:04:31 浏览: 178
以下是每行代码的注释:
```java
import java.util.Scanner; // 引入Scanner类
import java.util.Arrays; // 引入Arrays类
import java.util.Comparator; // 引入Comparator接口
import java.util.List; // 引入List接口
import java.util.ArrayList; // 引入ArrayList类
import java.util.stream.Collectors; // 引入Collectors类
class Main { // 定义Main类
public static void main(String[] args) { // 定义程序的入口方法
Scanner scanner = new Scanner(System.in); // 创建Scanner对象并关联控制台输入
int numberOfWineshops = scanner.nextInt(); // 读取一个整数,表示酒店的数量
int numberOfPickedWineshops = scanner.nextInt(); // 读取一个整数,表示要选取的酒店的数量
int referencePrice = scanner.nextInt(); // 读取一个整数,表示参考价格
int[] WineshopPrices = new int[numberOfWineshops]; // 创建一个整型数组,存储每个酒店的价格
for (int i = 0; i < numberOfWineshops; i++) { // 循环读取每个酒店的价格
WineshopPrices[i] = scanner.nextInt(); // 读取一个整数,表示当前酒店的价格,并存储到数组中
}
Arrays.sort(WineshopPrices); // 对酒店的价格进行排序
int[][] priceDifference = new int[numberOfWineshops][2]; // 创建一个二维整型数组,存储每个酒店的价格和它与参考价格的差值
for (int i = 0; i < numberOfWineshops; i++) { // 循环计算每个酒店与参考价格的差值,并存储到数组中
int price = WineshopPrices[i]; // 获取当前酒店的价格
priceDifference[i][0] = price; // 存储当前酒店的价格
priceDifference[i][1] = Math.abs(price - referencePrice); // 存储当前酒店与参考价格的差值
}
List<int[]> sortedPriceDifference = Arrays.stream(priceDifference) // 构造一个流,将二维数组转换为流
.sorted(Comparator.comparingInt(Wineshop -> Wineshop[1])) // 对流中的元素按照第二列进行排序
.collect(Collectors.toList()); // 将排序后的流转换为List
List<Integer> pickedWineshopPrices = new ArrayList<>(); // 创建一个整型List,存储选中的酒店的价格
for (int i = 0; i < numberOfPickedWineshops; i++) { // 循环选取指定数量的酒店
pickedWineshopPrices.add(sortedPriceDifference.get(i)[0]); // 将选中的酒店的价格存储到List中
}
pickedWineshopPrices.sort(Integer::compareTo); // 对选中的酒店的价格进行排序
for (int i = 0; i < pickedWineshopPrices.size(); i++) { // 循环输出选中的酒店的价格
System.out.print(pickedWineshopPrices.get(i)); // 输出当前酒店的价格
if (i != pickedWineshopPrices.size() - 1) { // 如果当前不是最后一个酒店
System.out.print(" "); // 输出一个空格
}
}
}
}
```
阅读全文