东营网签查询系统官方网站,南阳做玉器网站,wordpress怎么运行,wordpress推广联盟需求#xff1a; 放暑假了#xff0c;小王决定到某旅游景点游玩#xff0c;他在网上搜索到了各种价位的酒店#xff08;长度为n的数组A#xff09;#xff0c;他的心理价位是x元#xff0c;请帮他筛选出k个最接近x元的酒店#xff08;nk0#xff09;#xff…需求 放暑假了小王决定到某旅游景点游玩他在网上搜索到了各种价位的酒店长度为n的数组A他的心理价位是x元请帮他筛选出k个最接近x元的酒店nk0并由低到高打印酒店的价格。 1酒店价格数组A和小王的心理价位x均为整型数据(0 nkx 10000) 2优先选择价格最接近心理价位的酒店若两家酒店和心理价位差价相同则选择价格较低的酒店。比如100元和300元距离心理价位200元同样接近此时选择100元; 3酒店价格可能相同重复。输入描述第一行n, k, x第二行A[0] A[1] A[2]…A[n-1]输出描述由低到高打印筛选出的酒店价格编码
public class ReservationHotel {public static void main(String[] args) {//使用useDelimiter()方法输入一起输入Scanner sc new Scanner(System.in).useDelimiter(\\D);System.out.print(请输入:);int countsc.nextInt();int numbersc.nextInt();int moneysc.nextInt();//调用方法show(count,number,money,sc);}/**** param count 数组长度* param number 筛选个数* param money 目标价位*/public static void show(int count,int number,int money, Scanner sc ){//1集合存放各个酒店价格数据ListInteger hotelPriceList new ArrayList();System.out.print(请输入count个酒店价格);for (int i 0; i count ; i) {hotelPriceList.add(sc.nextInt());}// 2根据酒店价格从低到高排序Collections.sort(hotelPriceList,(o1, o2) - {//心理价位差价int dif1Math.abs(o1-money);int dif2Math.abs(o2-money);
// 比较不等if (dif1!dif2){return Integer.compare(dif1,dif2);}else{return Integer.compare(o1,o2);}});System.out.println(进行比较后的酒店价格:hotelPriceList.toString());//3将集合存放各个筛选出number个最接近money元的酒店ListInteger findPriceList new ArrayList();for (int i 0; i number ; i) {findPriceList.add(hotelPriceList.get(i)); //获取酒店筛选出数据}//4再按酒店价格从低到高排序Collections.sort(findPriceList);System.out.println(筛选出最接目标价位money元的酒店findPriceList.toString());}
}效果