定制排序
1. 冠亚军排名
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = Integer.parseInt(in.nextLine());
List<medal> list = new ArrayList<>();
for (int i = 0; i < N; i++) {
String[] split = in.nextLine().split(" ");
String country = split[0];
int goldCount = Integer.parseInt(split[1]);
int silverCount = Integer.parseInt(split[2]);
int bronzeCount = Integer.parseInt(split[3]);
list.add(new medal(country, goldCount, silverCount, bronzeCount));
}
list.stream().sorted((o1, o2) -> {
int goldCount1 = o1.goldCount;
int goldCount2 = o2.goldCount;
int silverCount1 = o1.silverCount;
int silverCount2 = o2.silverCount;
int bronzeCount1 = o1.bronzeCount;
int bronzeCount2 = o2.bronzeCount;
if (goldCount1 != goldCount2){
return goldCount2 - goldCount1;
}
if (silverCount1 != silverCount2){
return silverCount2 - silverCount1;
}
if (bronzeCount1 != bronzeCount2){
return bronzeCount2 - bronzeCount1;
}
return o1.country.compareTo(o2.country);
}).forEach(medal -> System.out.println(medal.country));
}
}
class medal{
String country;
int goldCount;
int silverCount;
int bronzeCount;
public medal(String country, int goldCount, int silverCount, int bronzeCount) {
this.country = country;
this.goldCount = goldCount;
this.silverCount = silverCount;
this.bronzeCount = bronzeCount;
}
}
LeetCode 179. 最大数【字符串拼接】
class Solution {
public String largestNumber(int[] nums) {
StringBuilder res = new StringBuilder();
String[] str = new String[nums.length];
for (int i = 0; i < nums.length; i++) {
str[i] = nums[i] + "";
}
Arrays.sort(str, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// A < B:则说明 o1 < o2 了
String A = o1 + o2;
String B = o2 + o1;
return B.compareTo(A); // 逆序
}
});
for (String s : str) {
res.append(s);
}
if (res.toString().matches("[0]+")){ // 都是 0 的用例
return "0";
}
return res.toString();
}
}
205. 数组组成的最小数字
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] split = in.nextLine().split(",");
List<String> list = new ArrayList<>();
for (String s : split) {
list.add(s);
}
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int len1= o1.length();
int len2= o2.length();
if (len1 != len2){
return len1 - len2;
}
return o1.compareTo(o2);
}
});
List<String> temp = new ArrayList<>();
for (int i = 0; i < 3 && i <= list.size() - 1; i++) {
temp.add(list.get(i));
}
Collections.sort(temp, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return (o1 + o2).compareTo(o2 + o1);
}
});
StringBuilder res = new StringBuilder();
for (String s : temp) {
res.append(s);
}
System.out.println(res);
}
}
300. 比赛
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static int row;
static int col;
static int[][] arr;
static Map<Integer, List<Integer>> map = new HashMap<>();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] split = in.nextLine().split(",");
row = Integer.parseInt(split[0]); // 评委
col = Integer.parseInt(split[1]); // 选手
if (col < 3 || col > 100){
System.out.println(-1);
return;
}
if (row < 3 || row > 10){
System.out.println(-1);
return;
}
arr = new int[row][col];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < row; i++) {
sb.append(in.nextLine() + ",");
}
String[] temp = sb.toString().substring(0, sb.length() - 1).split(",");
int index = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
arr[i][j] = Integer.parseInt(temp[index++]);
if (arr[i][j] < 1 || arr[i][j] > 10){
System.out.println(-1);
return;
}
}
}
for (int i = 0; i < col; i++) { // 遍历选手
List<Integer> list = new ArrayList<>();
for (int j = 0; j < row; j++) {
list.add(arr[j][i]);
}
Collections.sort(list, (o1, o2) -> o2 - o1);
map.put(i + 1, list);
}
StringBuilder res = new StringBuilder();
map.keySet().stream().sorted(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
List<Integer> list1 = map.get(o1);
List<Integer> list2 = map.get(o2);
int sum1 = 0;
int sum2 = 0;
for (Integer integer : list1) {
sum1 += integer;
}
for (Integer integer : list2) {
sum2 += integer;
}
if (sum1 != sum2){
return sum2 - sum1;
}
for (int i = 0; i < list1.size(); i++) {
if (list1.get(i) != list2.get(i)){
return list2.get(i) - list1.get(i); // 逆序排列【大的在前面!!!】
}
}
return 0;
}
}).limit(3).forEach(integer -> res.append(integer + ","));
System.out.println(res.toString().substring(0, res.length() - 1));
}
}
247. 寻找身高相近的小朋友
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static List<Integer> list = new ArrayList<>();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] split = in.nextLine().split(" ");
int height = Integer.parseInt(split[0]);
int count = Integer.parseInt(split[1]);
String[] temp = in.nextLine().split(" ");
for (int i = 0; i < count; i++) {
list.add(Integer.parseInt(temp[i]));
}
StringBuilder res = new StringBuilder();
list.stream().sorted(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int height1 = Math.abs(o1 - height);
int height2 = Math.abs(o2 - height);
if (height1 != height2){
return height1 - height2;
}
return o1 - o2;
}
}).forEach(integer -> res.append(integer + " "));
System.out.println(res.toString().trim());
}
}
156. 选修课
# 特殊用例:同一个学号出现多次!!!
01202021,75;01201033,95;01202008,80;01203006,90;01203088,100;01202008,70
01202008,70;01203088,85;01202111,80;01202021,75;01201100,88;01203088,100
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<Student> list = new ArrayList<>();
Map<String, Integer> map = new HashMap<>();
String[] split1 = in.nextLine().split(";");
for (String s : split1) {
String[] split = s.split(",");
map.put(split[0], Integer.parseInt(split[1]));
}
String[] split2 = in.nextLine().split(";");
for (String s : split2) {
String[] split = s.split(",");
String temp = split[0]; // 学号
int score = Integer.parseInt(split[1]);
if (!map.containsKey(temp)){
continue;
}
String classNo = temp.substring(0, 5);
list.add(new Student(temp, classNo, map.get(temp) + score));
}
if (list.size() == 0){
System.out.println("NULL");
return;
}
Map<String, String> res = new HashMap<>(); // 整体思路:按照正常排序,最后用 res 来收集结果就行!!!【班级(前五位),学号(全部)】
list.stream().sorted(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int classNo1 = Integer.parseInt(o1.classNo);
int classNo2= Integer.parseInt(o2.classNo);
int score1 = o1.score;
int score2 = o2.score;
if (classNo1 != classNo2){
return classNo1 - classNo2;
}
if (score1 != score2){
return score2 - score1;
}
return Integer.parseInt(o1.stuNo) - Integer.parseInt(o2.stuNo);
}
}).forEach(student -> {
String classNo = student.classNo;
String stuNo = student.stuNo;
String orDefault = res.getOrDefault(classNo, "");
if (!orDefault.contains(stuNo)){ // 处理特殊情况,同一个学号出现多次!!!
res.put(classNo, res.getOrDefault(classNo, "") + stuNo + ";");
}
});
res.keySet().stream().sorted(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return Integer.parseInt(o1) - Integer.parseInt(o2);
}
}).forEach(s -> {
System.out.println(s);
String temp = res.get(s);
System.out.println(temp.substring(0, temp.length() - 1));
});
}
}
class Student{
String stuNo;
String classNo;
int score;
public Student(String stuNo, String classNo, int score) {
this.stuNo = stuNo;
this.classNo = classNo;
this.score = score;
}
}
338. 统计射击比赛成绩
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = Integer.parseInt(in.nextLine());
Map<Integer, List<Integer>> map = new HashMap<>();
String[] split1 = in.nextLine().split(",");
String[] split2 = in.nextLine().split(",");
for (int i = 0; i < count; i++) {
int id = Integer.parseInt(split1[i]);
int score = Integer.parseInt(split2[i]);
List<Integer> orDefault = map.getOrDefault(id, new ArrayList<>());
orDefault.add(score);
map.put(id, orDefault);
}
Map<Integer, Integer> res = new HashMap<>();
for (Integer integer : map.keySet()) {
List<Integer> list = map.get(integer);
if (list.size() >= 3){
Collections.sort(list, (o1, o2) -> o2 - o1);
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += list.get(i);
}
res.put(integer, sum);
}
}
StringBuilder sb = new StringBuilder();
res.keySet().stream().sorted((o1, o2) -> {
int sum1 = res.get(o1);
int sum2 = res.get(o2);
if (sum1 != sum2){
return sum2 - sum1;
}
return o2 - o1;
}).forEach(integer -> sb.append(integer + ","));
System.out.println(sb.toString().substring(0, sb.length() - 1));
}
}