本文主要是介绍分治算法-球队循环赛日程安排-java实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*** 分治法-球队循环赛事日程安排,球队数量最好是2的n次方,不然日程安排会出现空隙* 采用分治法进行处理,将日程安排分成四个象限来处理*/public class RoundMatch {public void roundMatch(int[][] table, int n) {if (n == 1) {table[0][0] = 1;return;}int m = n / 2;// 先填充左上区域roundMatch(table, m);// 右上区域for (int i = 0; i < m; i++) {for (int j = m; j < n; j++) {table[i][j] = table[i][j - m] + m;}}// 左下区域for (int i = m; i < n; i++) {for (int j = 0; j < m; j++) {table[i][j] = table[i - m][j] + m;}}// 右下区域for (int i = m; i < n; i++) {for (int j = m; j < n; j++) {table[i][j] = table[i - m][j - m];}}}public static void main(String[] args) {RoundMatch roundMatch = new RoundMatch();int size = 8;int[][] table = new int[size][size];roundMatch.roundMatch(table, size);for (int i = 0; i < size; i++) {for (int j = 0; j < size; j++) {System.out.print(table[i][j] + " ");}System.out.println();}}}
结果打印:
1 2 3 4 5 6 7 8
2 1 4 3 6 5 8 7
3 4 1 2 7 8 5 6
4 3 2 1 8 7 6 5
5 6 7 8 1 2 3 4
6 5 8 7 2 1 4 3
7 8 5 6 3 4 1 2
8 7 6 5 4 3 2 1
这篇关于分治算法-球队循环赛日程安排-java实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!