Yaakov Lake 2021-04-04 20:10 采纳率: 100%
浏览 109
已采纳

java分治思想实现网球循环赛 包括奇数偶数情况

一、问题描述: 设有n个运动员要进行网球循环赛。设计一个满足以下要求的比赛日程表: (1)每个选手必须与其他n-1个选手各赛一次; (2)每个选手一天只能赛一次; (3)当n是偶数时,循环赛进行n-1天。当n是奇数时,循环赛进行n天。 能给个详细的源代码吗 谢谢了

  • 写回答

1条回答 默认 最新

  • allway2 2021-04-06 21:53
    关注
    package TennisTournamentSchedule;
    
    public class TennisTournamentSchedule {
    	public static void show(int[][] a, int size) {
    		int i, j;
    		System.out.println(
    				"Number before\":\"means the player. After the \":\" means the player's opponent in No.i day.\n");
    		System.out.print("\t");
    		for (j = 0; j <= a[0].length - 2; j++)
    			System.out.print("Day" + (j + 1) + "\t");
    		System.out.println();
    		for (i = 0; i <= size - 1; i++) {
    			for (j = 0; j <= a[i].length - 1; j++) {
    				if (j == 0)
    					System.out.print(a[i][j] + ":\t");
    				else
    					System.out.print(a[i][j] + "\t");
    			}
    			System.out.println();
    		}
    		System.out.println();
    	}
    
    	public static void replaceVirtual(int[][] a, int m) {
    		int i, j;
    		for (i = 0; i < a.length - 1; i++) {
    			for (j = 0; j <= a[0].length - 1; j++) {
    				if (a[i][j] == m)
    					a[i][j] = 0;
    			}
    		}
    	}
    
    	public static void copyeven(int[][] a, int m) {
    		int i, j;
    		for (j = 0; j < m; j++) {
    			for (i = 0; i < m; i++)
    				a[i + m][j] = a[i][j] + m;
    		}
    		for (j = m; j < 2 * m; j++) {
    			for (i = 0; i < m; i++)
    				a[i][j] = a[i + m][j - m];
    			for (i = m; i < 2 * m; i++)
    				a[i][j] = a[i - m][j - m];
    		}
    	}
    
    	public static void copyodd(int[][] a, int m) {
    		int i, j;
    		for (j = 0; j <= m; j++) {
    			for (i = 0; i < m; i++) {
    				if (a[i][j] != 0)
    					a[i + m][j] = a[i][j] + m;
    				else {
    					a[i + m][j] = i + 1;
    					a[i][j] = i + m + 1;
    				}
    			}
    		}
    
    		for (i = 0, j = m + 1; j < 2 * m; j++) {
    			a[i][j] = j + 1;
    			a[(a[i][j] - 1)][j] = i + 1;
    		}
    
    		for (i = 1; i < m; i++) {
    			for (j = m + 1; j < 2 * m; j++) {
    				a[i][j] = ((a[i - 1][j] + 1) % m == 0) ? a[i - 1][j] + 1 : m + (a[i - 1][j] + 1) % m;
    				a[(a[i][j] - 1)][j] = i + 1;
    			}
    		}
    	}
    
    	public static void copy(int[][] a, int m) {
    		if (m % 2 == 1)
    			copyodd(a, m / 2);
    		else
    			copyeven(a, m / 2);
    	}
    
    	public static void tournament(int[][] a, int num) {
    		if (num == 1) {
    		} else {
    			if (num % 2 == 0) {
    				tournament(a, num / 2);
    				copy(a, num);
    			} else {
    				tournament(a, num + 1);
    				replaceVirtual(a, num + 1);
    			}
    		}
    	}
    
    	public static void schedule(int[][] a, int num) {
    		int i;
    		for (i = 0; i <= a.length - 1; i++)
    			a[i][0] = i + 1;
    		tournament(a, num);
    	}
    
    	public static void main(String[] args) {
    		int i, j, size;
    		int[][] a = new int[0][0];
    
    		size = 7;
    		System.out.println("The number of players: " + size);
    
    		if (size % 2 == 0) {
    
    			a = new int[size][size];
    		} else if (size % 2 == 1) {
    
    			a = new int[size + 1][size + 1];
    
    		}
    
    		for (i = 0; i <= a.length - 1; i++) {
    			for (j = 0; j <= a[i].length - 1; j++) {
    				a[i][j] = 0;
    			}
    		}
    
    		schedule(a, size);
    		show(a, size);
    
    	}
    
    }

    The number of players: 7
    Number before":"means the player. After the ":" means the player's opponent in No.i day.

        Day1    Day2    Day3    Day4    Day5    Day6    Day7    
    1:    2    3    4    5    6    7    0    
    2:    1    4    3    6    5    0    7    
    3:    4    1    2    7    0    5    6    
    4:    3    2    1    0    7    6    5    
    5:    6    7    0    1    2    3    4    
    6:    5    0    7    2    1    4    3    
    7:    0    5    6    3    4    1    2    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 NeRF的代码复现以及人工智能入门
  • ¥15 centos如何做一个可以让ikuai连接的openvpn
  • ¥15 Protege 中的Entities下面的界面删除了如何复原
  • ¥15 scrapy爬虫求帮
  • ¥15 imageEnView绘图问题
  • ¥15 关于#python#的问题:您好可以加您一下联系方式吗,在复现的时候确实有点问题难以解决
  • ¥15 联想电脑重装系统时无法发现硬盘
  • ¥15 MATLAB与UR10e实体机械臂建立通讯
  • ¥15 c++题需要快一点不用opencv
  • ¥15 关于#java#的问题:想要咨询Flowable流程引擎框架的问题