public class Test1 { private int[] numbers = new int[] {1, 2, 3, 3, 4, 5}; public int n; private String lastResult = ""; private boolean validate(String s) { if (s.compareTo(lastResult) <= 0) { return false; } if (s.charAt(2) == '4') { return false; } if (s.indexOf("35") >= 0 || s.indexOf("53") >= 0) { return false; } return true; } public void list(String index, String result) { for (int i = 0; i < numbers.length; i++) { if (index.indexOf(i + 48) < 0) { String s = result + String.valueOf(numbers[i]); if (s.length() == numbers.length) { if (validate(s)) { System.out.println(s); lastResult = s; n++; } break; } list(index + String.valueOf(i), s); } } } public static void main(String[] args) { Test1 t = new Test1(); t.list("", ""); System.out.println("总数:" + t.n); } }
import java.util.*; public class Callsort { public static void main(String[] args) throws Exception{ String[] array = new String[]{"1","2","2","3","4","5"}; listAll(Arrays.asList(array),"",array.length);
}
public static void listAll(List<String> candidate, String prefix,int length){ if(prefix.length()==length&& prefix.lastIndexOf("35")==-1&& prefix.lastIndexOf("53")==-1&& prefix.indexOf("4")!=2) System.out.println(prefix); for( int i=0; i < candidate.size(); i++ ) { List<String> temp = new ArrayList<String>(candidate); listAll(temp, prefix+temp.remove(i),length); } } } 这个包你满意,又简单,又容易理解
再整理一下 import java.util.*; public class Callsort { static int row=0; public static void main(String[] args) throws Exception{ String[] array = new String[]{"1","2","2","3","4","5"}; listAll(Arrays.asList(array),"",array.length);
public static void main(String[] args) {
List<String> listTemp = new ArrayList<String>();
List<String> listRes = new ArrayList<String>();
getString("122345", listTemp);
for (int i = 0; i < listTemp.size(); i++) {
if (checkString(listTemp.get(i), listRes))
listRes.add(listTemp.get(i));
}
for (int i = 0; i < listRes.size(); i++) {
System.out.println(listRes.get(i));
}
System.out.println("原始的数目为:" + listTemp.size());
System.out.println("过滤的数目为:" + listRes.size());
} public static void getString(String str, List<String> list) { // 将传来的字符串的所有的排列放到List中
int len = str.length();
int[] arr = new int[len];
for (int i = 0; i < len; i++) {
arr[i] = 0;
}
while (arr[len - 1] != len) {
if (checkArray(arr)) {
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < len; i++) {
sb.append(str.charAt(arr[i]));
}
list.add(sb.toString());
}
arr[0]++;
for (int i = 0; i < len - 1; i++) {
if (arr[i] == len) {
arr[i] = 0;
arr[i + 1]++;
}
}
}
} public static boolean checkString(String str, List<String> list) { // 当前的字符串是否符合规则,并没有在列表list中
if (str.charAt(2) == '4')
return false;
if (str.contains("35") || str.contains("53"))
return false;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals(str))
return false;
}
return true;
} public static boolean checkArray(int[] arr) { // 检查是否使用了同一的字符
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (i != j && arr[i] == arr[j])
return false;
}
}
return true;
}
public static void main(String [] args){
for(int i1=1;i1<5;i1++){
for(int i2=1;i2<5;i2++){
if(i1*i2==15){
continue;
}
for(int i3=1;i3<5;i3++){
if(i3==4){
continue;
}
if(i2*i3==15){
continue;
}
for(int i4=1;i4<5;i4++){
if(i3*i4==15){
continue;
}
for(int i5=1;i5<5;i5++){
if(i4*i5==15){
continue;
}
System.out.println(i1+i2+i3+i4+i5);
}
}
}
}
}
}
然后,为什么如果我把最后输出时的“,”去掉,会不能正常显示呢?public static void main(String [] args){
int ar[] = {1,2,2,3,4,5};
for(int i0=0;i0<6;i0++){
for(int i1=0;i1<6;i1++){
if(i1==i0){
continue;
}
if(ar[i0]*ar[i1]==15){
continue;
}
for(int i2=0;i2<6;i2++){
if(i2==i1 || i2==i0){
continue;
}
if(ar[i2]==4){
continue;
}
if(ar[i1]*ar[i2]==15){
continue;
}
for(int i3=0;i3<6;i3++){
if(i3==i2 || i3==i1 || i3==i0){
continue;
}
if(ar[i2]*ar[i3]==15){
continue;
}
for(int i4=0;i4<6;i4++){
if(i4==i3 || i4==i2 || i4==i1 || i4==i0){
continue;
}
if(ar[i3]*ar[i4]==15){
continue;
}
for(int i5=0;i5<6;i5++){
if(i5==i4 || i5==i3 || i5==i2 || i5==i1 || i5==i0){
continue;
}
if(ar[i4]*ar[i5]==15){
continue;
}
System.out.println(ar[i0]+","+ar[i1]+","+ar[i2]+","+ar[i3]+","+ar[i4]+","+ar[i5]);
}
}
}
}
}
}
}
改成System.out.println(""+ar[i0]+ar[i1]+ar[i2]+ar[i3]+ar[i4]+ar[i5]);就可以了。。- - 再次声明:俺是菜鸟:-O
public class Test1
{
private int[] numbers = new int[]
{1, 2, 3, 3, 4, 5};
public int n;
private String lastResult = ""; private boolean validate(String s)
{
if (s.compareTo(lastResult) <= 0)
{
return false;
}
if (s.charAt(2) == '4')
{
return false;
}
if (s.indexOf("35") >= 0 || s.indexOf("53") >= 0)
{
return false;
}
return true;
} public void list(String index, String result)
{
for (int i = 0; i < numbers.length; i++)
{
if (index.indexOf(i + 48) < 0)
{
String s = result + String.valueOf(numbers[i]);
if (s.length() == numbers.length)
{
if (validate(s))
{
System.out.println(s);
lastResult = s;
n++;
}
break;
}
list(index + String.valueOf(i), s);
}
}
} public static void main(String[] args)
{
Test1 t = new Test1();
t.list("", "");
System.out.println("总数:" + t.n); }
}
public class Callsort {
public static void main(String[] args) throws Exception{
String[] array = new String[]{"1","2","2","3","4","5"};
listAll(Arrays.asList(array),"",array.length);
}
public static void listAll(List<String> candidate, String prefix,int length){
if(prefix.length()==length&&
prefix.lastIndexOf("35")==-1&&
prefix.lastIndexOf("53")==-1&&
prefix.indexOf("4")!=2)
System.out.println(prefix);
for( int i=0; i < candidate.size(); i++ ) {
List<String> temp = new ArrayList<String>(candidate);
listAll(temp, prefix+temp.remove(i),length);
}
}
}
这个包你满意,又简单,又容易理解
import java.util.*;
public class Callsort {
static int row=0;
public static void main(String[] args) throws Exception{
String[] array = new String[]{"1","2","2","3","4","5"};
listAll(Arrays.asList(array),"",array.length);
}
public static void listAll(List<String> candidate, String prefix,int length){
if(prefix.length()==length&&
prefix.lastIndexOf("35")==-1&& //如果出现3,5相连的不打印
prefix.lastIndexOf("53")==-1&& //如果出现5,3相连的不打印
prefix.indexOf("4")!=2) { //如果4出现在第三位不打印
System.out.print(prefix+" ");
row++;
if(row==15){ //每15个元素就换行
System.out.println();
row=0;
}
}
for( int i=0; i < candidate.size(); i++ ) {
List<String> temp = new ArrayList<String>(candidate);
listAll(temp, prefix+temp.remove(i),length);
}
}
}122345 122543 123245 123254 123425 123452 125234 125243 125423 125432 122345 122543 123245 123254 123425
123452 125234 125243 125423 125432 132245 132254 132425 132452 132524 132542 132245 132254 132425 132452
132524 132542 142325 142523 142325 142523 143225 143252 143225 143252 145223 145232 145223 145232 152234
152243 152324 152342 152423 152432 152234 152243 152324 152342 152423 152432 212345 212543 213245 213254
213425 213452 215234 215243 215423 215432 221345 221543 223145 223154 223415 223451 225134 225143 225413
225431 231245 231254 231425 231452 231524 231542 232145 232154 232415 232451 232514 232541 241325 241523
242315 242513 243125 243152 243215 243251 245123 245132 245213 245231 251234 251243 251324 251342 251423
251432 252134 252143 252314 252341 252413 252431 212345 212543 213245 213254 213425 213452 215234 215243
215423 215432 221345 221543 223145 223154 223415 223451 225134 225143 225413 225431 231245 231254 231425
231452 231524 231542 232145 232154 232415 232451 232514 232541 241325 241523 242315 242513 243125 243152
243215 243251 245123 245132 245213 245231 251234 251243 251324 251342 251423 251432 252134 252143 252314
252341 252413 252431 312245 312254 312425 312452 312524 312542 312245 312254 312425 312452 312524 312542
315224 315242 315224 315242 315422 315422 321245 321254 321425 321452 321524 321542 322145 322154 322415
322451 322514 322541 325124 325142 325214 325241 325412 325421 321245 321254 321425 321452 321524 321542
322145 322154 322415 322451 322514 322541 325124 325142 325214 325241 325412 325421 341225 341252 341225
341252 341522 341522 342125 342152 342215 342251 342512 342521 342125 342152 342215 342251 342512 342521
345122 345122 345212 345221 345212 345221 412325 412523 412325 412523 413225 413252 413225 413252 415223
415232 415223 415232 421325 421523 422315 422513 423125 423152 423215 423251 425123 425132 425213 425231
421325 421523 422315 422513 423125 423152 423215 423251 425123 425132 425213 425231 431225 431252 431225
431252 431522 431522 432125 432152 432215 432251 432512 432521 432125 432152 432215 432251 432512 432521
451223 451232 451223 451232 451322 451322 452123 452132 452213 452231 452312 452321 452123 452132 452213
452231 452312 452321 512234 512243 512324 512342 512423 512432 512234 512243 512324 512342 512423 512432
513224 513242 513224 513242 513422 513422 521234 521243 521324 521342 521423 521432 522134 522143 522314
522341 522413 522431 523124 523142 523214 523241 523412 523421 521234 521243 521324 521342 521423 521432
522134 522143 522314 522341 522413 522431 523124 523142 523214 523241 523412 523421 541223 541232 541223
541232 541322 541322 542123 542132 542213 542231 542312 542321 542123 542132 542213 542231 542312 542321
543122 543122 543212 543221 543212 543221
* :用1、2、2、3、4、5这六个数字,用java写一个main函数, 打印出所有不同的排列,如:512234、412345等,
* 要求:"4"不能在第三位,"3"与"5"不能相连.
*/
public static void main(String[] a) {
long start;
System.out.println("结果是:");
int count = 0; for (start = 122345; start <= 543221; start++) {
String s = String.valueOf(start);
if (Validate(s)) {
if ((s.indexOf("35") == -1) && (s.indexOf("53") == -1) // 如果没要找到字符返回-1
&& (s.charAt(2) != '4')) {
System.out.println(s);
count++;
}
}
} System.out.println("最后结果共" + count);
} public static boolean Validate(String l) { // 保证数字只出现这几个数字
int[] a = new int[] { 0, 0, 0, 0, 0 };
for (int i = 0; i < 6; i++) {
if (l.charAt(i) == '1')
a[0]++;
if (l.charAt(i) == '2')
a[1]++;
if (l.charAt(i) == '3')
a[2]++;
if (l.charAt(i) == '4')
a[3]++;
if (l.charAt(i) == '5')
a[4]++;
}
// 如果1出现1次,2出现2次,3,4,5各出现一次,返回true,
if (a[0] == 1 && a[1] == 2 && a[2] == 1 && a[3] == 1 && a[4] == 1)
return true;
else
return false;
}
}这绝对正解
to:r4141496091
你的结果有重复的,,,396多了
import java.util.*;
public class Callsort {
static int count=0;
static TreeSet ts=new TreeSet();
public static void main(String[] args) throws Exception{
String[] array = new String[]{"1","2","2","3","4","5"};
listAll(Arrays.asList(array),"",array.length);
Iterator it=ts.iterator();
while (it.hasNext()){
System.out.println((String)it.next());
}
System.out.println("结果数:"+ts.size());
}
public static void listAll(List<String> candidate, String prefix,int length){
if(prefix.length()==length&&
prefix.lastIndexOf("35")==-1&&
prefix.lastIndexOf("53")==-1&&
prefix.indexOf("4")!=2) {
ts.add(prefix);
}
for( int i=0; i < candidate.size(); i++ ) {
List<String> temp = new ArrayList<String>(candidate);
listAll(temp, prefix+temp.remove(i),length);
}
}
}忘记了22不可以重复的
这个包正确的递归版
我只需要一个set集合就可以过来一半,因为22重复,可以减少一半
但是你的很离谱
public static void main(String[] args){
int[] num={0,1,2,2,3,4,5};
int[] mod=new int[]{1,7,49,343,2401,16807,117649};
double len=Math.pow(num.length,num.length-1);
// for(int i=0,base=num.length;i<=mod.length;i++){
// mod[i]=Math.pow(base,i);
// System.out.println(mod[i]);
// }
try{
PrintStream oldOut,cout=new PrintStream(new FileOutputStream("d:/out.txt"));
oldOut=System.out;
System.setOut(cout); String usedIdx,item;
int idx,count=0;
for(int i=0;i<len;i++){
usedIdx="";
item="";
//元素不允许重复使用,48+n的目的是将数字转字符
for(int j=num.length-2;j>=0;j--){
idx=i%mod[j+1]/mod[j];
if(usedIdx.indexOf(48+idx)!=-1){
continue;
}else{
usedIdx+=idx;
item+=idx>0?num[idx]+"":"";
}
}
if(item.length()<6 //去除子排列
||(item.indexOf('4')==3) //4不能在第三位
||(item.indexOf("35")!=-1)){//3与5不能相连
continue;
}else{
System.out.println(item);
count++;
}
}
System.setOut(oldOut);
System.out.println(count);
}catch(Exception e){
e.printStackTrace();
}
}
}
public class TestPermutation {
public static void main(String[] args){
int[] num={0,1,2,2,3,4,5};//0表示不选,用于产生子排列
int[] mod=new int[]{1,7,49,343,2401,16807,117649};
double len=Math.pow(num.length,num.length-1);
// //自动计算mod,需要修改
// for(int i=0,base=num.length;i<=mod.length;i++){
// mod[i]=Math.pow(base,i);//
// } String usedIdx,item;
int idx,count=0;
for(int i=0;i<len;i++){
usedIdx="";
item="";
//元素不允许重复使用,48+n的目的是将数字转字符
for(int j=num.length-2;j>=0;j--){
idx=i%mod[j+1]/mod[j];
if(usedIdx.indexOf(48+idx)!=-1){
continue;
}else{
usedIdx+=idx;
item+=idx>0?num[idx]+"":"";
}
}
if(item.length()<6 //去除子排列
||(item.indexOf('4')==3) //4不能在第三位
||(item.indexOf("35")!=-1)){//3与5不能相连
continue;
}else{
System.out.println(item);
count++;
}
} System.out.println(count);
}
}
int min = 122345;
int max = 543221;
for(int i=min;i<=max;i++) {
String str = String.valueOf(i);
if(str.split("")[2].equals("4")) {
continue;
}
if(str.indexOf("35")!=-1) {
continue;
}
if(str.indexOf("1")!=-1 && str.indexOf("2")!=-1 && str.indexOf("3")!=-1
&& str.indexOf("4")!=-1 && str.indexOf("5")!=-1) {
String newStr = str.replaceFirst("2", "");
if(newStr.indexOf("2")!=-1) {
System.out.println(str);
}
}
}
A(6,6) / 2 - 2 * C(1,5) * A(4,4)/ 2 - A(5,5) / 2 + 2 * C(1,3) * A(3,3) / 2
=360 - 120 - 60 + 18 = 198