/
SamMih
/
Algorithms_HOMEWORK3_Sort
Обзор
Документация
Войти
/
SamMih
/
Algorithms_HOMEWORK3_Sort
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Main.java
58 строк
2 KB
SamMih
Algorithms_HOMEWORK3_Sort
25 июл 2025, 11:23
25 июл 2025, 11:23
9fb8917
Код
Авторство
О чём код?
import java.io.*; import java.util.Arrays; public class Main { public static void main(String[] args) throws IOException { int[][] teams = { { 45, 31, 24, 22, 20, 17, 14, 13, 12, 10 }, { 31, 18, 15, 12, 10, 8, 6, 4, 2, 1 }, { 51, 30, 10, 9, 8, 7, 6, 5, 2, 1 } }; int[] nationalTeam = mergeAll(teams); System.out.println(Arrays.toString(nationalTeam)); // [51, 45, 31, 31, 30, 24, 22, 20, 18, 17] } /** Метод для слияния всех команд в одну национальную */ public static int[] mergeAll(int[][] teams) { if (teams.length == 0) { return new int[0]; } int[] result = teams[0]; for (int i = 1; i < teams.length; i++) { result = merge(result, teams[i]); } return result; } /** Метод для слияния двух команд в одну */ public static int[] merge(int[] teamA, int[] teamB) { int[] merged = new int[teamA.length + teamB.length]; int i = 0; int j = 0; int k = 0; while (i < teamA.length && j < teamB.length) { if (teamA[i] >= teamB[j]) { merged[k++] = teamA[i++]; } else { merged[k++] = teamB[j++]; } } while (i < teamA.length) { merged[k++] = teamA[i++]; } while (j < teamB.length) { merged[k++] = teamB[j++]; } int[] result = new int[Math.min(10, merged.length)]; for (int x = 0; x < result.length; x++) { result[x] = merged[x]; } return result; } }