0到9,10个数字中随机取5个排列算法

陕K流年 2011-11-12 06:04:14
从0,1,2,3,4,5,6,7,8,9十个数字中任意选择5个数字排列(不可以可以是重复数字)的组合分别有多少?具体分别有哪些组合?
1.数字的先后顺序忽略(1、2、3、4、5)==(5、4、3、2、1)
2.每组中5个数字不能重复

java如何实现
...全文
1999 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
陕K流年 2011-11-13
  • 打赏
  • 举报
回复
package com.tst;

import java.util.*;

/**************************************************************
* 文件名称: Combination
* 功能描述: 从0,1,2,3,4,5,6,7,8,9十个数字中任意选择5个数字排列(不可以可以是重复数字)的组合
* 创建日期: 2011-11-13
* 创建地址: 西安
* 作者: Eric
**************************************************************/

public class Combination {

// 组合算法
// 本程序的思路是开一个数组,其下标表示1到m个数,
// 数组元素的值为1表示其下标代表的数被选中,为0则没选中。
// 首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。
// 然后从左到右扫描数组元素值的"10"组合,找到第一个"10"组合后将其变为
// "01"组合,同时将其(在本算法中具体指i的位置)左边的所有"1"全部移动到数组的最左端。
// 当第一个"1"移动到数组的m-n的位置,即n个"1"全部移动到最右端时,就得
// 到了最后一个组合。

/**
* @param a:组合数组
* @param m:生成组合长度
* @return :所有可能的组合数组列表
*/
private List<int[]> combination(int[] a, int m) {

Combination c = new Combination();

List<int[]> list = new ArrayList<int[]>();

int n = a.length;

boolean end = false; // 是否是最后一种组合的标记

// 生成辅助数组。首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。
int[] tempNum = new int[n];

for (int i = 0; i < n; i++) {
if (i < m) {
tempNum[i] = 1;

} else {
tempNum[i] = 0;
}
}

//printVir(tempNum);// 打印首个辅助数组

list.add(c.createResult(a, tempNum, m));// 添加第一种默认组合

int k = 0;// 标记位
int j = 0;

while (!end) {

boolean findFirst = false;

boolean swap = false;

// 然后从左到右扫描数组元素值的组合

for (int i = 0; i < n; i++) {

j = 0;

if (!findFirst && tempNum[i] == 1) {
k = i;
findFirst = true;
}

if (tempNum[i] == 1 && tempNum[i + 1] == 0) {
tempNum[i] = 0;
tempNum[i + 1] = 1;
swap = true;
for (j = 0; j < i - k; j++) { // 同时将其左边的所有"1"全部移动到数组的最左端。
tempNum[j] = tempNum[k + j];
}
for (j = i - k; j < i; j++) {
tempNum[j] = 0;
}
if (k == i && i + 1 == n - m) {// 假如第一个"1"刚刚移动到第n-m+1个位置,则终止整个寻找
end = true;
}
}
if (swap) {
break;
}
}
//printVir(tempNum);// 打印辅助数组
list.add(c.createResult(a, tempNum, m));// 添加下一种默认组合
}
return list;
}

// 根据辅助数组和原始数组生成结果数组
public int[] createResult(int[] a, int[] temp, int m) {
int[] result = new int[m];
int j = 0;
for (int i = 0; i < a.length; i++) {
if (temp[i] == 1) {
result[j] = a[i];
//System.out.println("result[" + j + "]:" + result[j]);
j++;
}
}
return result;
}

// 打印整组数组
public void print(List<int[]> list) {
System.out.println("具体组合结果为:");
for (int i = 0; i < list.size(); i++) {
int[] temp = (int[]) list.get(i);
for (int j = 0; j < temp.length; j++) {
//java.text.DecimalFormat df = new java.text.DecimalFormat("00");// 将输出格式化
//System.out.print(df.format(temp[j]) + " ");
System.out.print("["+temp[j]+"]");
}
System.out.println();
}
}

// 打印辅助数组的方法
public void printVir(int[] a) {
System.out.println("生成的辅助数组为:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
}
System.out.println();
}

public static void main(String[] args) {
int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // 整数数组
int m = 5; // 待取出组合的个数
Combination c = new Combination();
List<int[]> list = c.combination(a, m);
c.print(list);
System.out.println("一共" + list.size() + "组!");
}

}

2,408

社区成员

发帖
与我相关
我的任务
社区描述
高性能计算
社区管理员
  • 高性能计算社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧