用1、2、2、3、4、5这六个数字 , 用java写一个main函数,打印出所有不同的排列...

package ArithmeticTest;

import java.util.HashSet;
import java.util.Set;

/**
* 题目:用1、2、2、3、4、5这六个数字,
* 用java写一个main函数,打印出所有不同的排列,
* 如:512234、412345等,要求:”4″不能在第三位,”3″与”5″不能相连.
* @author hbj
*
*/
public class Arithmetic2 {
static Set<String> intSet = new HashSet<String>();
public static void doSomething(Integer[] array,int count,String result){
if(count==6){
if(result.indexOf('4')!=2&&!result.contains("35")&&!result.contains("53")){
intSet.add(result);
}
}else{
for(int i=0;i<array.length;i++){
if(array[i]!=0){
int temCount = count;
String _tem = result;
_tem += array[i]+"";
Integer[] _array = array.clone();
_array[i]=0;
doSomething(_array,++temCount,_tem);
}
}
}
}



public static void main(String[] args){
Integer[] array = new Integer[]{1,2,2,3,4,5};
Arithmetic2.doSomething(array, 0, "");
Integer c=1;
for(String s :intSet){
System.out.println((c++)+": "+s);
}
}

}


结果是 198个,不知道对了没有
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值