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

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 c# 调用python报错Python.Runtime.PythonException:“No module named '_ctypes,如何解决?
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥60 为什么使用python对地震数据进行umap降维后,数据成图会出现不连续的现象
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 安卓qpython向ksweb服务器post文件失败