`

数N的全排列,两队对阵问题

 
阅读更多

 

 

 

/*
	 * 题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,
	 * c说他不和x,z比,请编程序找出三队赛手的名单。
	 */
	public static void print20() {
		long start = System.currentTimeMillis();
		char a, b, c;
		for (a = 'x'; a <= 'z'; a++) {
			for (b = 'x'; b <= 'z'; b++) {
				if (a != b) {
					for (c = 'x'; c <= 'z'; c++) {
						if (a != c && b != c) {
							if (a != 'x' && c != 'x' && c != 'z') {
								System.out.println(a + "," + b + "," + c);
							}
						}
					}
				}
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("共用了" + (end - start) + "毫秒。");
	}

	/*
	 * 题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,
	 * c说他不和x,z比,请编程序找出三队赛手的名单。
	 * 
	 * 此方法可以实现abc和xyz数目不同时的对阵,但abc的长度要小于等于xyz的长度;
	 */
	public static void print21(String[] abc, String[] xyz) {
		long start = System.currentTimeMillis();
		// 二维数组orders的值1代表x,2代表y,3代表z;
		// 共有abc数组的长度列,每列代表与数组abc列对应的值
		// 如序列为[1,2,3],则对手分别为:a-x,b-y,c-z
		int col = abc.length;
		int[][] orders = getOrder(1, xyz.length, col);
		int row = orders.length;
		String[][] rs = new String[row][col];
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				rs[i][j] = abc[j] + xyz[orders[i][j] - 1];
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("共用了" + (end - start) + "毫秒。");
		for (int i = 0; i < row; i++) {
			boolean flag = true;

			for (int j = 0; j < col; j++) {
				if (rs[i][j].equals("ax") || rs[i][j].equals("cx")
						|| rs[i][j].equals("cz")) {
					flag = false;
					break;
				}
			}

			if (flag) {
				System.out.println(Arrays.toString(rs[i]));
			}
		}
	}

	/*
	 * 从1到N的全部数列
	 */
	public static int[][] getOrder(int n) {
		return getOrder(1, n, n);
	}

	/*
	 * 从start到end中任意选择count个数字组成的序列
	 * 如从1到3的排列有[1,2,3]、[1,3,2]、[2,1,3]、[2,3,1],[3,1,2]、[3,2,1]
         * 思路(当N为3时,即求数字1、2、3的所有排列):
         * 1.要得到数字1、2、3的全部排列,只需要得到以1开始的所有排列123和132
         * 2.将123各位+1,得到231(当相加的和超过最大值时变为最小值,即个位的3+1=4>3,时变为1),再+1,得到312
         * 3.将132同步骤2,得到213和321
         * 4.如何得到步骤1中的123和132呢,只需要得到23和32,即数字2、3的全排列
         * 5.同步骤1,要得到数字2、3的全部排列,只需要得到以2开始的所有排列23
         * 6.同步骤2,得到32
         * 7.如何得到步骤4中的23呢,只需要得到3,即数字3的全排列
	 */
	public static int[][] getOrder(int start, int end, int count) {
		if (count == 1) {
			int tempN = end - start + 1;
			int[][] now = new int[tempN][count];
			for (int i = 0; i < tempN; i++) {
				now[i][0] = start + i;
			}
			return now;
		} else {
			int row = 1;
			int tempN = end - start + 1;
			for (int i = 0; i < count; i++) {
				row *= tempN--;
			}
			int[][] now = new int[row][count];
			int[][] pre = getOrder(start + 1, end, count - 1);
			int preLength = pre.length;
			for (int i = 0; i < preLength; i++) {
				now[i][0] = start;
				for (int j = 0; j < pre[i].length; j++) {
					now[i][j + 1] = pre[i][j];
				}
			}
			for (int i = 1; i < row / preLength; i++) {
				for (int j = 0; j < preLength; j++) {
					for (int k = 0; k < count; k++) {
						int value = now[(i - 1) * preLength + j][k] + 1;
						if (value > end) {
							value = start;
						}
						now[i * preLength + j][k] = value;
					}
				}
			}
			return now;
		}
	}

	public static void main(String[] args) {

		long start = System.currentTimeMillis();
		int[][] orders = T2.getOrder(6);
		long end = System.currentTimeMillis();
		System.out.println("共" + orders.length + "项:");
		for (int i = 0; i < orders.length; i++) {
			System.out.println(Arrays.toString(orders[i]));
		}
		System.out.println("共用了" + (end - start) + "毫秒。");
		System.out.println("------------------");
		T2.print20();
		System.out.println("------------------");
		String[] abc = { "a", "b", "c" };
		String[] xyz = { "x", "y", "z" };
		T2.print21(abc, xyz);
		System.out.println("------------------");
		String[] abc2 = { "a", "b", "c" };
		String[] xyz2 = { "x", "y", "z", "w" };
		T2.print21(abc2, xyz2);
	}

 

输出:

共720项:
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 6, 5]
[1, 2, 3, 5, 6, 4]
[1, 2, 3, 5, 4, 6]
[1, 2, 3, 6, 4, 5]
[1, 2, 3, 6, 5, 4]
[1, 2, 4, 5, 6, 3]
[1, 2, 4, 5, 3, 6]
[1, 2, 4, 6, 3, 5]
[1, 2, 4, 6, 5, 3]
[1, 2, 4, 3, 5, 6]
[1, 2, 4, 3, 6, 5]
[1, 2, 5, 6, 3, 4]
[1, 2, 5, 6, 4, 3]
[1, 2, 5, 3, 4, 6]
[1, 2, 5, 3, 6, 4]
[1, 2, 5, 4, 6, 3]
[1, 2, 5, 4, 3, 6]
[1, 2, 6, 3, 4, 5]
[1, 2, 6, 3, 5, 4]
[1, 2, 6, 4, 5, 3]
[1, 2, 6, 4, 3, 5]
[1, 2, 6, 5, 3, 4]
[1, 2, 6, 5, 4, 3]
[1, 3, 4, 5, 6, 2]
[1, 3, 4, 5, 2, 6]
[1, 3, 4, 6, 2, 5]
[1, 3, 4, 6, 5, 2]
[1, 3, 4, 2, 5, 6]
[1, 3, 4, 2, 6, 5]
[1, 3, 5, 6, 2, 4]
[1, 3, 5, 6, 4, 2]
[1, 3, 5, 2, 4, 6]
[1, 3, 5, 2, 6, 4]
[1, 3, 5, 4, 6, 2]
[1, 3, 5, 4, 2, 6]
[1, 3, 6, 2, 4, 5]
[1, 3, 6, 2, 5, 4]
[1, 3, 6, 4, 5, 2]
[1, 3, 6, 4, 2, 5]
[1, 3, 6, 5, 2, 4]
[1, 3, 6, 5, 4, 2]
[1, 3, 2, 4, 5, 6]
[1, 3, 2, 4, 6, 5]
[1, 3, 2, 5, 6, 4]
[1, 3, 2, 5, 4,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1, 2, 5]
[6, 4, 3, 1, 5, 2]
[6, 4, 3, 2, 5, 1]
[6, 4, 3, 2, 1, 5]
[6, 5, 1, 2, 3, 4]
[6, 5, 1, 2, 4, 3]
[6, 5, 1, 3, 4, 2]
[6, 5, 1, 3, 2, 4]
[6, 5, 1, 4, 2, 3]
[6, 5, 1, 4, 3, 2]
[6, 5, 2, 3, 4, 1]
[6, 5, 2, 3, 1, 4]
[6, 5, 2, 4, 1, 3]
[6, 5, 2, 4, 3, 1]
[6, 5, 2, 1, 3, 4]
[6, 5, 2, 1, 4, 3]
[6, 5, 3, 4, 1, 2]
[6, 5, 3, 4, 2, 1]
[6, 5, 3, 1, 2, 4]
[6, 5, 3, 1, 4, 2]
[6, 5, 3, 2, 4, 1]
[6, 5, 3, 2, 1, 4]
[6, 5, 4, 1, 2, 3]
[6, 5, 4, 1, 3, 2]
[6, 5, 4, 2, 3, 1]
[6, 5, 4, 2, 1, 3]
[6, 5, 4, 3, 1, 2]
[6, 5, 4, 3, 2, 1]
共用了1毫秒。
------------------
z,x,y
共用了0毫秒。
------------------
共用了0毫秒。
[az, bx, cy]
------------------
共用了0毫秒。
[ay, bz, cw]
[ay, bx, cw]
[az, bw, cy]
[az, bx, cy]
[az, bx, cw]
[az, by, cw]
[aw, bx, cy]
[aw, bz, cy]

 

 又一思路(来自网络)

/*
	 * 求数组中数字的全部排列
	 */
	public static void print41(String prefix, int[] nums) {
		if (nums.length == 1) {
			System.out.println(prefix + nums[0]);
		}
		for (int i = 0; i < nums.length; i++) {
			int[] b = new int[nums.length - 1];
			System.arraycopy(nums, 0, b, 0, i);
			System.arraycopy(nums, i + 1, b, i, nums.length - i - 1);
			print41(prefix + nums[i], b);
		}
	}

	public static void main(String[] args) {
		int[] nums = { 1, 2, 3, 5 };
		T2.print41("", nums);
	}

输出:

1235
1253
1325
1352
1523
1532
2135
2153
2315
2351
2513
2531
3125
3152
3215
3251
3512
3521
5123
5132
5213
5231
5312
5321

 

分享到:
评论

相关推荐

    opencv 比赛对阵图自动生成

    7. **优化与扩展**:在实际应用中,可能需要考虑对阵图的动态更新、多语言支持、适应不同屏幕尺寸等问题。可以通过优化代码结构,使用面向对象编程提高可维护性;引入响应式设计,确保对阵图在不同设备上都能良好...

    对阵图编辑软件

    对阵图编辑软件是一种专为组织各类比赛而设计的实用工具,它可以帮助用户快速、便捷地创建和管理比赛的对阵情况。此类软件的核心功能通常包括随机对阵生成、批量参赛者管理和自动编排等,以满足不同规模比赛的需求。...

    适合各种比赛——对阵图编辑软件

    【对阵图编辑软件】是一款专为组织各类对抗性比赛而设计的专业工具,它集成了创建、编辑和管理比赛对阵图的多种功能。软件的核心在于帮助用户便捷地制作出清晰、直观的比赛流程图表,以适应不同规模和类型的赛事,...

    比赛安排1

    在这个问题中,有2的n次幂(n)个球队参与单循环比赛,总共需要在2的n次幂减1天内完成所有比赛,每个队伍每天只能参加一场对阵不同的对手的比赛。 首先,我们需要理解单循环比赛的概念。在单循环比赛中,每个队伍都...

    棒球赛问题的队伍对阵信息数据集

    ### 棒球赛问题的队伍对阵信息数据集解析 #### 数据集概述 该数据集包含了一定量的棒球队伍对阵信息,适用于测试算法或模型在处理大规模棒球比赛对阵时的性能。根据描述,数据集至少覆盖了54支不同规模的队伍,并且...

    南非世界杯对阵表(EXCEL函数已配好)

    这个表格不仅展示了比赛的对阵情况,而且它利用了Excel的内置函数功能,使得用户只需要输入比分,就能自动计算出各队的积分、排名等相关信息。 描述 "用EXCEL做的世界杯对阵表,内嵌函数已经做好,只需填上比分万事...

    纯js+html实现羽毛球比赛抽签功能,生成对阵图,并导出PDF和Excel文件

    1.纯js+html实现抽签功能,导入excel文件,得到抽签结果,将抽签table导出为PDF(html2canvas+jsPdf)、Excel...2.导入excel文件,生成小组赛对阵图,并导出PDF, 在excel文件中,手动添加晋级队伍,动态生成对称图。

    用制作淘汰赛对阵表.doc

    制作淘汰赛对阵表的Word文档操作指南 在这篇文章中,我们将学习如何使用Word制作淘汰赛对阵表。这是一个非常实用的技能,特别是在学校或企业中需要制作比赛对阵表时。 首先,我们需要了解什么是淘汰赛对阵表。淘汰...

    最新 .Net 比赛对阵图编码 jqery

    网上很难找到的此类代码 自己编写 喜欢的给个好评

    单循环比赛编排

    2. 比赛场数:单循环比赛的场数,可用下面的公式计算(简单的数学组合公式):比赛场数= 队数*(队数-1)/2。如 6 个队或 7 个队参加比赛,则比赛场数为:6 *(6-1)/2 =15(场) 7*(7-1)/2 =21(场) 单循环比赛的编排方法...

    50道程序题

    此题可通过辗转相除法求得两数的最大公约数,进而计算最小公倍数,辗转相除法是一种古老而有效的算法。 #### 程序7:字符串类型统计 输入一行字符,统计其中英文字母、空格、数字和其他字符的数量。此题可通过读取...

    比赛对阵表模板.doc

    【比赛对阵表模板.doc】是一个组织比赛活动时常用的工具,用于规划和管理竞赛流程,确保比赛的公正性和有序性。比赛对阵表通常包括比赛的各个阶段、参赛队伍或选手、比赛时间、比赛场地以及相关规则等内容。以下是...

    scheduling a Tournament.pdf

    例如,如果队1在周五对阵队2,在周六对阵队3,那么剩下的两支队伍(队3和队4)必须在周五进行比赛,而队2和队4则需在周六进行比赛。周日的比赛将是队1对阵队4以及队2对阵队3。这样的赛程安排见下表: | | 周五 | ...

    小学奥数组合问题.doc

    例如,握手问题中,如果甲先和乙握手,再和丙握手,与甲先和丙握手再和乙握手是不同的排列,但在组合问题中,这两个握手方式被视为相同。 3. 完全平方公式应用:在计算握手次数时,利用组合公式C(n, 2) = n(n-1)/2...

    赢最多和赢最少问题 java实现

    这个过程需要嵌套两层循环,外层循环代表总轮数,内层循环代表己方未出场的马匹数。 Java代码实现可能如下: ```java int[][] dp = new int[3][3]; for (int i = 0; i ; i++) { for (int j = 0; j ; j++) { // ...

    用word制作淘汰赛对阵表.doc

    在Word中创建淘汰赛对阵表是一项实用的技巧,尤其对于组织体育赛事或任何需要分组比赛的活动。以下是如何利用Word来制作淘汰赛对阵表的详细步骤: 1. **页面设置**:首先,你需要调整页面的布局以适应对阵表的需求...

    X年巴西世界杯小组赛对阵时间表.pdf

    【X年巴西世界杯小组赛对阵时间表】是教育领域的一个重要资料,主要展示了2014年在巴西举办的世界杯足球赛小组赛的比赛安排。这份时间表详细列出了各参赛队伍的对阵情况以及比赛的具体时间,对于了解赛事进程、组织...

    四川省成都市高中数学第一章计数原理1.2.1排列1导学提纲学案新人教A版选修2_320180727121

    对于这个问题,可以使用组合公式C(n, k)*2,其中C(n, k)表示组合数,2是因为每场对阵有主客场两种情况。 4. 分组循环淘汰赛的排列问题:如世界杯小组赛,32支球队分8组,每组4队,每队和其他3队各赛一场,这里...

    算法作业+足球比赛问题

    根据他们过去比赛的成绩,得出每个队与另一个队对阵时取胜的概率表: 甲 乙 丙 丁 甲 - 0.1 0.3 0.5 乙 0.9 - 0.7 0.4 丙 0.7 0.3 - 0.2 丁 0.5 0.6 0.8 - 数据含义:甲对乙的取胜概率为0.1,丙对乙的胜率为0.3...

Global site tag (gtag.js) - Google Analytics