#美团笔试#
老哥们送外卖这个哪里错了,直接排序不行吗
public class Main {
static class Pay {
int vals;
int weight;
int id;
public Pay(int vals, int weight, int id) {
this.vals = vals;
this.weight = weight;
this.id = id;
}
int pay() {
return vals + weight * 2;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
Pay[] pays = new Pay[n];
for (int i = 0; i < n; i++) {
pays[i] = new Pay(sc.nextInt(), sc.nextInt(), i);
}
Arrays.sort(pays, (p1, p2) -> {
int t = p2.pay() - p1.pay();
if (t == 0) {
return p1.id - p2.id;
}
return t;
});
for (int i = 1; i < m; i++) {
System.out.println((pays[i].id + 1) + " ");
}
}
}
老哥们送外卖这个哪里错了,直接排序不行吗
public class Main {
static class Pay {
int vals;
int weight;
int id;
public Pay(int vals, int weight, int id) {
this.vals = vals;
this.weight = weight;
this.id = id;
}
int pay() {
return vals + weight * 2;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
Pay[] pays = new Pay[n];
for (int i = 0; i < n; i++) {
pays[i] = new Pay(sc.nextInt(), sc.nextInt(), i);
}
Arrays.sort(pays, (p1, p2) -> {
int t = p2.pay() - p1.pay();
if (t == 0) {
return p1.id - p2.id;
}
return t;
});
for (int i = 1; i < m; i++) {
System.out.println((pays[i].id + 1) + " ");
}
}
}
全部评论
用剑指topK的方法,做到55,不知道漏了哪个情况
我也是这样写的,很迷惑
相关推荐
01-23 15:35
University of Edinburgh 嵌入式软件工程师 不知道怎么取名字_:嵌入式其实不是很好干的,要学的东西比较多的,你这个c stm32都是比较基础的了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
2025-12-15 14:16
门头沟学院 Java 点赞 评论 收藏
分享
测开劝退第4人:双9啊,校徽打出来,字体拉最大 点赞 评论 收藏
分享

