java m取n 重复 不重复 排列组合 for循环嵌套递归

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

package com.file.read;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.Random;

public class CharacterCompose {

public static SimpleDateFormat formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");

/**

*

* **/

public static void main(String[] args) {

char[] chs = { 'a', 'b', 'c', '1', '2', '3', '4', '5', 'd', 'e', 'f',

'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',

's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '6', '7', '8', '9' };

//// char[] chs = { 'A', 'B', 'C', 'D'};

// duplicate(chs) ;

// notDuplicate(chs);

// int a = 1838265625;

// Random r = new Random();

// duplicate(chs, r.nextInt(a)+1) ;

// long numb = 1;

// for(int i = 0; i < 6; i++) {

// numb = numb * chs.length;

// }

// System.out.println("重复:::"+numb);

//

// a = 1168675200;

// r = new Random();

// notDuplicate(chs, r.nextInt(a)+1) ;

// numb = 1;

// for(int i = 0; i < 6; i++) {

// numb = numb * (chs.length-i);

// }

// System.out.println("不重复:::"+numb);

// char[] chs = { 'A', 'B', 'C', 'D', 'E', 'F'};

//总数

int total = 0;

//取n个字符

int n = 6;

///////////////////////////////////////////////////////////////////////////////////

//不能重复

boolean ifNotSame = true;

if(n > 1 && n <= chs.length) {

for(int i = 0; i < chs.length; i++) {

total = total+loop(0, chs.length, chs, String.valueOf(chs[i]), 1, n-1,ifNotSame);

}

}

String desc = "重复";

if(ifNotSame) {

desc = "不重复";

}

System.out.println(chs.length+"取" + n + desc + "的排列组合个数为 " + total);

///////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////

/****/

total=0;

ifNotSame = false;

if(n > 1 && n <= chs.length) {

for(int i = 0; i < chs.length; i++) {

total = total+loop(0, chs.length, chs, String.valueOf(chs[i]), 1, n-1,ifNotSame);

}

}

desc = "重复";

if(ifNotSame) {

desc = "不重复";

}

System.out.println(chs.length+"取" + n + desc + "的排列组合个数为 " + total);

/////////////////////////////////////////////////////////////////////////////////// }

//for循环嵌套递归

public static int loop(int start, int end, char[] chs, String msg, int loopIndex, int loopEnd,boolean ifNotSame) {

int total = 0;

for(int i = start; i < end; i++) {

if(loopIndex == loopEnd) {

if(ifNotSame && msg.indexOf(String.valueOf(chs[i])) != -1) {

continue;

}

相关文档
最新文档