看见大家循环套循环,圈圈套圈圈的写法,我实在是忍无可忍了,6个数字还好,给你一百个数字,是不是准备百环大作战了? 我来实现一下我之前讲的基本思路: 1 把问题归结为图结构的遍历问题。实际上6个数字就是六个结点,把六个结点连接成无向连通图,对于每一个结点求这个图形的遍历路径,所有结点的遍历路径就是最后对这6个数字的排列组合结果集。 2 显然这个结果集还未达到题目的要求。从以下几个方面考虑: 1. 3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足改条件,然后再遍历图。 2. 不能有重复: 考虑到有两个2,明显会存在重复结果,可以把结果集放在TreeSet中过滤重复结果 3. 4不能在第三位: 仍旧在结果集中去除满足此条件的结果。采用二维数组定义图结构,最后的代码是:import java.util.Iterator; import java.util.TreeSet;public class TestQuestion {private String[] b = new String[]{"1", "2", "2", "3", "4", "5"}; private int n = b.length; private boolean[] visited = new boolean[n]; private int[][] a = new int[n][n]; private String result = ""; private TreeSet set = new TreeSet();public static void main(String[] args) { new TestQuestion().start(); }private void start() {// Initial the map a[][] for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) { a[i][j] = 0; } else { a[i][j] = 1; } } }// 3 and 5 can not be the neighbor. a[3][5] = 0; a[5][3] = 0;// Begin to depth search. for (int i = 0; i < n; i++) { this.depthFirstSearch(i); }// Print result treeset. Iterator it = set.iterator(); while (it.hasNext()) { String string = (String) it.next(); // "4" can not be the third position. if (string.indexOf("4") != 2) { System.out.println(string); } } }private void depthFirstSearch(int startIndex) { visited[startIndex] = true; result = result + b[startIndex]; if (result.length() == n) { // Filt the duplicate value. set.add(result); } for(int j = 0; j < n; j++) { if (a[startIndex][j] == 1 && visited[j] == false) { depthFirstSearch(j); } else { continue; } }// restore the result value and visited value after listing a node. result = result.substring(0, result.length() -1); visited[startIndex] = false; } }可以结贴了。
arbiter(同济流氓) 的结果还是有4在第三位。。
这个代码在其它帖子里已经验证过了,绝对没问题的。你把结果贴上来看,好好检查一下,你是不是跑了我的代码// Print result treeset. Iterator it = set.iterator(); while (it.hasNext()) { String string = (String) it.next(); // "4" can not be the third position. if (string.indexOf("4") != 2) { System.out.println(string); } } }打印结果集的代码里已经去掉4在第三位的情况了
arbiter(同济流氓)的算法确实很妙
偶也贴个程序,比较的短。也是基于递归的遍历,不过没有用图结构。 public class Assemble { public void getAssemble(String result, Vector<Character> source) { if ( (result.length() == 3) && (result.charAt(2) == '4') ) return;
if ( result.endsWith("35") || result.endsWith("53")) return;
if ( result.length() == 6 ) { System.out.println(result); return; }
for (int i=0; i<source.size(); i++) { Vector<Character> t = (Vector<Character>)source.clone(); t.remove(i); getAssemble(result + source.get(i), t); } }
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Assemble as = new Assemble(); Vector<Character> v = new Vector<Character>(); v.add('1'); v.add('2'); v.add('2'); v.add('3'); v.add('4'); v.add('5'); as.getAssemble("", v);
System.out.println("Done"); }}
大家好,只有10分钟时间,我估计还是用for循环来做比较实惠快速,毕竟只要能把题目做出来就OK了
For循环的方法 public class Test2 { public static void main(String args[]) { char[] c = {'1','2','2','3','4','5'}; for (int i = 0; i < c.length; i++ ) { for (int j = 0; j < c.length; j++) { if (i == j || (c[i]=='3' && c[j]=='5')) continue; for (int k = 0; k < c.length; k++) { if (k==i || k==j || (c[j]=='3' && c[k]=='5') || c[k] =='4') { continue; } for (int m = 0; m < c.length; m++) { if (m == k || m == i || m == j || (c[k]=='3' && c[m]==5)) { continue; } for (int n = 0; n < c.length; n++) { if (n == m || n == k || n == i || n == j || (c[m]=='3' && c[n]=='5')) { continue; } for (int l = 0; l < c.length; l++) { if (l == n || l == m || l == k || l == j || l == i || (c[n] == '3' && c[l] == '5')) { continue; } System.out.println(c[i]+""+c[j]+""+c[k]+""+c[m]+""+c[n]+""+c[l]); } } } } } } } }
递归方法 public class Test2 {
public int[] getNextDifPos(int[] before, int add) { int [] res = new int[before.length+1]; for (int i = 0; i < before.length; i++) { res[i] = before[i]; } res[res.length-1] = add; return res; }
public int getNextPos(int[] pos) { for (int i =0 ; i < pos.length; i++) { if (pos[i]!=-1){ return i; } } return -1; }
public void getStr(int[] before,char[] chars) { if (before.length == chars.length) { StringBuffer str = new StringBuffer(); for (int i = 0; i < before.length; i++) { str.append(chars[before[i]]); } //加判断条件 if (str.charAt(2)=='4' || str.indexOf("35") >=0) { return ; } System.out.println(str.toString()); } else if (before.length < chars.length) { int[] pos = new int[chars.length]; for (int i = 0; i < before.length; i++) { pos[before[i]] = -1; } int nextPos; while ( (nextPos=getNextPos(pos)) != -1) { pos[nextPos] = -1; getStr(getNextDifPos(before,nextPos),chars); } } }
public static void main(String args[]) { char[] c = {'1','2','2','3','4','5'}; int[] l = {}; Test2 t = new Test2(); t.getStr(l,c); } }
package find;import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List;public class Combination { public static void main(String[] args) throws Exception { int[] array = new int[] { 1, 2, 2, 3, 4, 5 }; List result=new ArrayList(); find(array, new int[0],result); for (int i = 0; i < result.size(); i++) { int[] resultArray=(int[])result.get(i); System.out.println(i+"\t=:"+Arrays.toString(resultArray)); } }
public static void find(final int[] restArray, final int[] inArray, List result){ if(restArray.length == 1){ int now = restArray[restArray.length-1]; if(inArray[inArray.length-1] == 3 && now == 5){ return; }else if(inArray[inArray.length-1] == 5 && now == 3){ return; } int[] resultArray=new int[inArray.length +1]; System.arraycopy(inArray, 0, resultArray, 0, inArray.length); resultArray[inArray.length]=now; result.add(resultArray); }else{ for (int i = 0; i < restArray.length; i++) { int now = restArray[i]; //check 3rd is not 4, skip if( inArray.length == 2 && now == 4){ continue; } //check 3,5 order if( inArray.length > 0){ if(inArray[inArray.length-1] == 3 && now == 5){ continue; }else if(inArray[inArray.length-1] == 5 && now == 3){ continue; } } int[] nextRestArray=new int[restArray.length -1]; int[] nextInArray=new int[inArray.length +1]; System.arraycopy(restArray,0, nextRestArray, 0, i); System.arraycopy(restArray,i+1, nextRestArray, i, nextRestArray.length-i); System.arraycopy(inArray, 0, nextInArray, 0, inArray.length); nextInArray[inArray.length]=now; find(nextRestArray, nextInArray, result); } } }}
还有就是如果考虑1、2、2、3、4、5里有两个2 则程序如下: package find;import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List;public class Combination { public static void main(String[] args) throws Exception { int[] array = new int[] { 1, 2, 2, 3, 4, 5 }; List result=new ArrayList(); find(array, new int[0],result);
for (int i = 0; i < result.size(); i++) { int[] resultArray=(int[])result.get(i); System.out.println(i+"\t=:"+Arrays.toString(resultArray)); } }
public static void find(final int[] restArray, final int[] inArray, List result){ if(restArray.length == 1){ int now = restArray[restArray.length-1]; if(inArray[inArray.length-1] == 3 && now == 5){ return; }else if(inArray[inArray.length-1] == 5 && now == 3){ return; } int[] resultArray=new int[inArray.length +1]; System.arraycopy(inArray, 0, resultArray, 0, inArray.length); resultArray[inArray.length]=now; result.add(resultArray); }else{ boolean first2=false; boolean second2=false; for (int i = 0; i < restArray.length; i++) { int now = restArray[i]; if(now == 2){ if(first2 == false) first2 = true; else second2 = true; } if(second2){ first2=false; second2=false; continue; }
//check 3rd is not 4, skip if( inArray.length == 2 && now == 4){ continue; } //check 3,5 order if( inArray.length > 0){ if(inArray[inArray.length-1] == 3 && now == 5){ continue; }else if(inArray[inArray.length-1] == 5 && now == 3){ continue; } } int[] nextRestArray=new int[restArray.length -1]; int[] nextInArray=new int[inArray.length +1]; System.arraycopy(restArray,0, nextRestArray, 0, i); System.arraycopy(restArray,i+1, nextRestArray, i, nextRestArray.length-i); System.arraycopy(inArray, 0, nextInArray, 0, inArray.length); nextInArray[inArray.length]=now; find(nextRestArray, nextInArray, result); } } }}
不晓得对不对 你们看一下 public void print(List nums1, List nums2) { if (nums1 != null && nums2 != null) { ArrayList list = new ArrayList(); String temp = ""; String temp1 = ""; String temp2 = ""; for (int i = 0; i < nums1.size(); i++) { for (int j = 0; j < nums2.size(); j++) { temp = nums1.get(i).toString() + nums2.get(j).toString(); temp1 = nums1.get(i).toString(); temp2 = nums2.get(j).toString(); { char[] tmp = temp.toCharArray(); boolean isUsable = true; for (int k = 0; k < tmp.length; k++) { if (isDup(temp, tmp[k])) { isUsable = false; break; } if (k < tmp.length - 1) { if (Integer.parseInt(String.valueOf(tmp[k])) + Integer.parseInt(String.valueOf(tmp[k+1])) == 8) { isUsable = false; break; } } } if (isUsable) { list.add(temp); if (temp.length() == 6 && temp.charAt(3) == '4') { System.out.println(temp); } } } } } if (temp.length() <6){ if (temp1.length() == temp2.length()) { print(list, nums1); } else { print(list, nums1); print(list, nums2); } } } else if (nums1 != null) { print(nums1, nums1); } else if (nums2 != null) { print(nums2, nums2); } } public boolean isDup(String str, char ch) { int start = str.indexOf(ch); int end = str.lastIndexOf(ch);
if (start != end) return true; else return false; }
直接从ECLIPSE里面COPY出来的 没想到这么乱 可以把程序拷贝运行 一共有64个值 不知道对不对 public class Test { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); list.add("5"); list.add("6"); new Test().print(list, null); } }
CpRcF(尘埃)兄弟有的地方写的不全,我改拉下程序。下面应该符合楼主的要求import java.io.FileWriter; import java.util.regex.Pattern; import java.util.regex.Matcher;public class Test { public static void main(String[] args) throws Exception { FileWriter out = new FileWriter("result.txt"); for (int i = 122345; i <= 543221; i++) { String s = String.valueOf(i); if (s.indexOf("35") >= 0 || s.indexOf("53") >= 0) { continue; } else{ //必须是1,2,3,4,5之间的数字 Pattern p = Pattern.compile("^[1-5]{6}$"); Matcher m = p.matcher(s); boolean b = m.matches(); if (b) { //第三位不能为4 Pattern pat = Pattern.compile("^[1-5]{2}[^4][1-5]{3}$"); Matcher mat = pat.matcher(s); boolean bool = mat.matches(); if (bool) { s = s + "\r\n"; out.write(s, 0, s.length()); } } } } out.close(); } }
package test2;public class Combination { /**
* @param args
*/
private static int i, j, k, l, m; public static void main(String[] args) {
// TODO Auto-generated method stub
int sum=0;
int[] array = { 0, 0, 0, 0, 0 };
boolean find = false;
int p = 0;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= 5; j++) {
if (j == i)
continue;
array[0] = i;
for (k = 1; k <= 5; k++) {
if (k == j || k == i)
continue;
array[1] = j;
for (l = 1; l <= 5; l++) {
if (l == i || l == j || l == k || k == 4)
continue;
array[2] = k;
for (m = 1; m <= 5; m++) {
if (m == i || m == j || m == k || m == l)
continue;
array[3] = l;
array[4] = m;
while (find == false && p <= 4) {
if (array[p] == 3 || array[p] == 5) {
if ((p + 1) <= 4) {
if (array[p + 1] == 3
|| array[p + 1] == 5) {
find = true;
break;
}
} }
p++;
}
if (find == false) {
sum++;
print(array);
}else
{
find=false;
}
p=0;
}
}
}
} }
System.out.println(sum);
} public static void print(int[] array) { for (int count = 0; count <= 4; count++)
System.out.print(array[count]);
System.out.println(); }
}
http://community.csdn.net/Expert/topic/5068/5068524.xml?temp=.2323725
我来实现一下我之前讲的基本思路:
1 把问题归结为图结构的遍历问题。实际上6个数字就是六个结点,把六个结点连接成无向连通图,对于每一个结点求这个图形的遍历路径,所有结点的遍历路径就是最后对这6个数字的排列组合结果集。
2 显然这个结果集还未达到题目的要求。从以下几个方面考虑:
1. 3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足改条件,然后再遍历图。
2. 不能有重复: 考虑到有两个2,明显会存在重复结果,可以把结果集放在TreeSet中过滤重复结果
3. 4不能在第三位: 仍旧在结果集中去除满足此条件的结果。采用二维数组定义图结构,最后的代码是:import java.util.Iterator;
import java.util.TreeSet;public class TestQuestion {private String[] b = new String[]{"1", "2", "2", "3", "4", "5"};
private int n = b.length;
private boolean[] visited = new boolean[n];
private int[][] a = new int[n][n];
private String result = "";
private TreeSet set = new TreeSet();public static void main(String[] args) {
new TestQuestion().start();
}private void start() {// Initial the map a[][]
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
a[i][j] = 0;
} else {
a[i][j] = 1;
}
}
}// 3 and 5 can not be the neighbor.
a[3][5] = 0;
a[5][3] = 0;// Begin to depth search.
for (int i = 0; i < n; i++) {
this.depthFirstSearch(i);
}// Print result treeset.
Iterator it = set.iterator();
while (it.hasNext()) {
String string = (String) it.next();
// "4" can not be the third position.
if (string.indexOf("4") != 2) {
System.out.println(string);
}
}
}private void depthFirstSearch(int startIndex) {
visited[startIndex] = true;
result = result + b[startIndex];
if (result.length() == n) {
// Filt the duplicate value.
set.add(result);
}
for(int j = 0; j < n; j++) {
if (a[startIndex][j] == 1 && visited[j] == false) {
depthFirstSearch(j);
} else {
continue;
}
}// restore the result value and visited value after listing a node.
result = result.substring(0, result.length() -1);
visited[startIndex] = false;
}
}可以结贴了。
Iterator it = set.iterator();
while (it.hasNext()) {
String string = (String) it.next();
// "4" can not be the third position.
if (string.indexOf("4") != 2) {
System.out.println(string);
}
}
}打印结果集的代码里已经去掉4在第三位的情况了
public class Assemble { public void getAssemble(String result, Vector<Character> source)
{
if ( (result.length() == 3) && (result.charAt(2) == '4') )
return;
if ( result.endsWith("35") || result.endsWith("53"))
return;
if ( result.length() == 6 )
{
System.out.println(result);
return;
}
for (int i=0; i<source.size(); i++)
{
Vector<Character> t = (Vector<Character>)source.clone();
t.remove(i);
getAssemble(result + source.get(i), t);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Assemble as = new Assemble();
Vector<Character> v = new Vector<Character>();
v.add('1'); v.add('2'); v.add('2');
v.add('3'); v.add('4'); v.add('5');
as.getAssemble("", v);
System.out.println("Done");
}}
public class Test2 {
public static void main(String args[]) {
char[] c = {'1','2','2','3','4','5'};
for (int i = 0; i < c.length; i++ ) {
for (int j = 0; j < c.length; j++) {
if (i == j || (c[i]=='3' && c[j]=='5'))
continue;
for (int k = 0; k < c.length; k++) {
if (k==i || k==j || (c[j]=='3' && c[k]=='5') || c[k] =='4') {
continue;
}
for (int m = 0; m < c.length; m++) {
if (m == k || m == i || m == j || (c[k]=='3' && c[m]==5)) {
continue;
}
for (int n = 0; n < c.length; n++) {
if (n == m || n == k || n == i || n == j || (c[m]=='3' && c[n]=='5')) {
continue;
}
for (int l = 0; l < c.length; l++) {
if (l == n || l == m || l == k || l == j || l == i || (c[n] == '3' && c[l] == '5')) {
continue;
}
System.out.println(c[i]+""+c[j]+""+c[k]+""+c[m]+""+c[n]+""+c[l]);
}
}
}
}
}
}
}
}
public class Test2 {
public int[] getNextDifPos(int[] before, int add) {
int [] res = new int[before.length+1];
for (int i = 0; i < before.length; i++) {
res[i] = before[i];
}
res[res.length-1] = add;
return res;
}
public int getNextPos(int[] pos) {
for (int i =0 ; i < pos.length; i++) {
if (pos[i]!=-1){
return i;
}
}
return -1;
}
public void getStr(int[] before,char[] chars) {
if (before.length == chars.length) {
StringBuffer str = new StringBuffer();
for (int i = 0; i < before.length; i++) {
str.append(chars[before[i]]);
}
//加判断条件
if (str.charAt(2)=='4' || str.indexOf("35") >=0) {
return ;
}
System.out.println(str.toString());
} else if (before.length < chars.length) {
int[] pos = new int[chars.length];
for (int i = 0; i < before.length; i++) {
pos[before[i]] = -1;
}
int nextPos;
while ( (nextPos=getNextPos(pos)) != -1) {
pos[nextPos] = -1;
getStr(getNextDifPos(before,nextPos),chars);
}
}
}
public static void main(String args[]) {
char[] c = {'1','2','2','3','4','5'};
int[] l = {};
Test2 t = new Test2();
t.getStr(l,c);
}
}
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;public class Combination {
public static void main(String[] args) throws Exception {
int[] array = new int[] { 1, 2, 2, 3, 4, 5 };
List result=new ArrayList();
find(array, new int[0],result);
for (int i = 0; i < result.size(); i++) {
int[] resultArray=(int[])result.get(i);
System.out.println(i+"\t=:"+Arrays.toString(resultArray));
}
}
public static void find(final int[] restArray, final int[] inArray, List result){
if(restArray.length == 1){
int now = restArray[restArray.length-1];
if(inArray[inArray.length-1] == 3 && now == 5){
return;
}else if(inArray[inArray.length-1] == 5 && now == 3){
return;
}
int[] resultArray=new int[inArray.length +1];
System.arraycopy(inArray, 0, resultArray, 0, inArray.length);
resultArray[inArray.length]=now;
result.add(resultArray);
}else{
for (int i = 0; i < restArray.length; i++) {
int now = restArray[i];
//check 3rd is not 4, skip
if( inArray.length == 2 && now == 4){
continue;
}
//check 3,5 order
if( inArray.length > 0){
if(inArray[inArray.length-1] == 3 && now == 5){
continue;
}else if(inArray[inArray.length-1] == 5 && now == 3){
continue;
}
}
int[] nextRestArray=new int[restArray.length -1];
int[] nextInArray=new int[inArray.length +1];
System.arraycopy(restArray,0, nextRestArray, 0, i);
System.arraycopy(restArray,i+1, nextRestArray, i, nextRestArray.length-i);
System.arraycopy(inArray, 0, nextInArray, 0, inArray.length);
nextInArray[inArray.length]=now;
find(nextRestArray, nextInArray, result);
}
}
}}
则程序如下:
package find;import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;public class Combination {
public static void main(String[] args) throws Exception {
int[] array = new int[] { 1, 2, 2, 3, 4, 5 };
List result=new ArrayList();
find(array, new int[0],result);
for (int i = 0; i < result.size(); i++) {
int[] resultArray=(int[])result.get(i);
System.out.println(i+"\t=:"+Arrays.toString(resultArray));
}
}
public static void find(final int[] restArray, final int[] inArray, List result){
if(restArray.length == 1){
int now = restArray[restArray.length-1];
if(inArray[inArray.length-1] == 3 && now == 5){
return;
}else if(inArray[inArray.length-1] == 5 && now == 3){
return;
}
int[] resultArray=new int[inArray.length +1];
System.arraycopy(inArray, 0, resultArray, 0, inArray.length);
resultArray[inArray.length]=now;
result.add(resultArray);
}else{
boolean first2=false;
boolean second2=false;
for (int i = 0; i < restArray.length; i++) {
int now = restArray[i];
if(now == 2){
if(first2 == false) first2 = true;
else second2 = true;
}
if(second2){
first2=false;
second2=false;
continue;
}
//check 3rd is not 4, skip
if( inArray.length == 2 && now == 4){
continue;
}
//check 3,5 order
if( inArray.length > 0){
if(inArray[inArray.length-1] == 3 && now == 5){
continue;
}else if(inArray[inArray.length-1] == 5 && now == 3){
continue;
}
}
int[] nextRestArray=new int[restArray.length -1];
int[] nextInArray=new int[inArray.length +1];
System.arraycopy(restArray,0, nextRestArray, 0, i);
System.arraycopy(restArray,i+1, nextRestArray, i, nextRestArray.length-i);
System.arraycopy(inArray, 0, nextInArray, 0, inArray.length);
nextInArray[inArray.length]=now;
find(nextRestArray, nextInArray, result);
}
}
}}
public class Ok { /**
* @param args
*/
public static void main(String[] args)
{
for(int i=111111;i<=555555;i++)
{
String a=String.valueOf(i);
String[] b=a.split("");
int flag=0;
if(b[2].equalsIgnoreCase("4"))
{
flag=1;
}
if(flag!=1)
{
for(int j=0;j<b.length;j++)
{
if(b[j].equalsIgnoreCase("6"))
{
flag=1;
break;
}
if(b[j].equalsIgnoreCase("7"))
{
flag=1;
break;
}
if(b[j].equalsIgnoreCase("8"))
{
flag=1;
break;
}
if(b[j].equalsIgnoreCase("9"))
{
flag=1;
break;
}
if(b[j].equalsIgnoreCase("0"))
{
flag=1;
break;
}
if(j<b.length-1)
{
if((b[j]+b[j+1]).equalsIgnoreCase("35")||(b[j]+b[j+1]).equalsIgnoreCase("53"))
{
flag=1;
break;
}
}
}
}
if(flag==0)
{
System.out.println(i);
}
} }}
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
FileWriter out = new FileWriter("result.txt");
int count = 0;
for(int i = 122345;i <= 543221;i++) {
String s = new String(i + " , ");
if(s.indexOf("4") == 2){
continue;
} else if(s.indexOf("35") >= 0 || s.indexOf("53") >= 0) {
continue;
} else {
count ++;
out.write(s,0,s.length());
}
}
System.out.println("find : " + count);
out.close();
}
}
----------------------------------------------------------------------------------人家是要"一个main函数"................
------ 以下是签名 ------
SoftDN.com, 提供免费的项目管理平台:SVN仓库、需求管理、BUG跟踪、任务管理、在线论坛、网站空间、项目存储及完整的备份等。欢迎网友光临建立项目,开创自己的梦想。
地址:http://www.softdn.com http://www.softdn.cn
public void print(List nums1, List nums2)
{
if (nums1 != null && nums2 != null)
{
ArrayList list = new ArrayList();
String temp = "";
String temp1 = "";
String temp2 = "";
for (int i = 0; i < nums1.size(); i++)
{
for (int j = 0; j < nums2.size(); j++)
{
temp = nums1.get(i).toString() + nums2.get(j).toString();
temp1 = nums1.get(i).toString();
temp2 = nums2.get(j).toString();
{
char[] tmp = temp.toCharArray();
boolean isUsable = true;
for (int k = 0; k < tmp.length; k++)
{
if (isDup(temp, tmp[k]))
{
isUsable = false;
break;
} if (k < tmp.length - 1)
{
if (Integer.parseInt(String.valueOf(tmp[k]))
+ Integer.parseInt(String.valueOf(tmp[k+1])) == 8)
{
isUsable = false;
break;
}
}
} if (isUsable)
{
list.add(temp);
if (temp.length() == 6 && temp.charAt(3) == '4')
{
System.out.println(temp);
}
}
}
}
}
if (temp.length() <6){
if (temp1.length() == temp2.length())
{
print(list, nums1);
} else
{
print(list, nums1);
print(list, nums2);
}
}
} else if (nums1 != null)
{
print(nums1, nums1);
} else if (nums2 != null)
{
print(nums2, nums2);
}
} public boolean isDup(String str, char ch)
{
int start = str.indexOf(ch);
int end = str.lastIndexOf(ch);
if (start != end)
return true;
else
return false;
}
可以把程序拷贝运行 一共有64个值 不知道对不对
public class Test
{
public static void main(String[] args)
{
ArrayList list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
list.add("6");
new Test().print(list, null);
}
}
public static void main(String[] args) {
Character[] numbers = new Character[] {'1', '2', '2', '3', '4', '5'};
Collection<String> combinations = combine(Arrays.asList(numbers));
System.out.printf("共计%d个\n", combinations.size());
for(String buffer: combinations) {
System.out.println(buffer);
}
} private static Set<String> combine(Collection<Character> characters) {
if(characters.size() == 1) {
String value = characters.iterator().next().toString();
return Collections.singleton(value);
}
Set<String> returnValue = new TreeSet();
//遍历ccharacters中的每一个字符,将剩余的字符作为一个集体
for(Character c: characters) { Collection<Character> left = new LinkedList(characters);
left.remove(c); Collection<String> combinations = combine(left);
for(String buffer: combinations) {
String newBuffer = buffer + c;
if(filter(newBuffer)) {
returnValue.add(newBuffer);
}
}
}
return returnValue;
} private static boolean filter(String value) {
int length = value.length();
if(length < 2) {
return true;
}
if(value.endsWith("35") || value.endsWith("53")) {
return false;
}
if(length < 4) {
return true;
}
if(value.charAt(3) == '4') {
return false;
}
return true;
}}
{
public static void main(String[] args)
{
char[] a="12345".toCharArray();
char[] b=new char[a.length];
for(int i=0;i<5;i++)
{
b[0]=a[i];
drop(a,i);
for(int j=0;j<4;j++)
{
b[1]=a[j];
drop(a,j);
for(int k=0;k<3;k++)
{
b[2]=a[k];
drop(a,k);
for(int l=0;l<2;l++)
{
b[3]=a[l];
drop(a,l);
b[4]=a[0];
System.out.println(new String(b).toString());
a="12345".toCharArray();
}
}
}
} }
static void drop(char[] a,int i)
{
for(int j=i+1;j<a.length;j++)
{
a[j-1]=a[j];
}
}
}
{
public static void main(String[] args)
{
char[] a="12345".toCharArray();
char[] b=new char[a.length];
for(int i=0;i<5;i++)
{
b[0]=a[i];
drop(a,i);
for(int j=0;j<4;j++)
{
b[1]=a[j];
drop(a,j);
for(int k=0;k<3;k++)
{
b[2]=a[k];
drop(a,k);
for(int l=0;l<2;l++)
{
b[3]=a[l];
drop(a,l);
b[4]=a[0];
System.out.println(new String(a).toString());
a="12345".toCharArray();
}
}
}
}
}
static void drop(char[] a,int i)
{
char x=a[i];
for(int j=i+1;j<a.length;j++)
{
a[j-1]=a[j];
}
a[a.length-1]=x;
}
}
这次应该没有错了吧
* @param args
*/
public static void main(String[] args) {
sort("", bits);
} private static void sort(String prefix, int[] a) {
if (a.length == 1) {
System.out.println(prefix + a[0]);
}
for (int i = 0; i < a.length; i++) {
sort(prefix + a[i], copy(a, i));
}
}
private static int[] copy(int[] a,int index){
int[] b = new int[a.length-1];
System.arraycopy(a, 0, b, 0, index);
System.arraycopy(a, index+1, b, index, a.length-index-1);
return b;
}
indexOf(String str, int fromIndex) 函数来确定是否将{1,2,2,3,4,5}都包含进去了~
就OK佩服CpRcF(尘埃)兄
google permute algorithm
我记得以前背过,不用图算法,代码很短。
现在忘了。呵呵。
public static int count=0;
public Untitled1() {
}
/**
* 递归处理
* @param buffer char[]
* @param col int
* @param printBuffer char[]
*/
private static void print(char[] buffer,int col,char[] printBuffer){
int i,j,z;
char[] tmpBuffer;
char[] prnBuffer=new char[6];
for(i=0; i<buffer.length; i++){
printBuffer[col]=buffer[i];
if(printBuffer[2]=='4')
continue;
if(col>1){
if (printBuffer[col] == '5' && printBuffer[col - 1] == '3')
continue;
if (printBuffer[col] == '3' && printBuffer[col - 1] == '5')
continue;
}
prnBuffer=String.valueOf(printBuffer,0,printBuffer.length).toCharArray();
if (buffer.length==1){
System.out.println(prnBuffer);
}
tmpBuffer=new char[buffer.length-1];
for(j=0,z=0; j<buffer.length; j++){
if (j!=i)
tmpBuffer[z++]=buffer[j];
}
if(col<6){
print(tmpBuffer, col + 1, prnBuffer);
count++;
}
}
}
public static void main(String[] args) {
char[] buffer=new char[]{'1','2','2','3','4','5'};
char[] printBuffer=new char[6];
Untitled1 u=new Untitled1();
u.print(buffer,0,printBuffer);
System.out.println("总数为:"+u.count);
}
}
import java.util.*;
public class ComSort {
private ArrayList res;
public ComSort(){
res=new ArrayList();
}
public static void main(String[] args){
ArrayList array=new ArrayList();
StringBuffer sb=new StringBuffer();
for(int i=1;i<6;i++){
array.add(i);
}
ComSort cs=new ComSort();
cs.compute(array,sb);
ArrayList res=cs.getArray();
ArrayList remove=new ArrayList();
for(Object o:res){
String value=o.toString();
if((value.indexOf("4")==2)||(value.indexOf("45")!=-1)){
remove.add(value);
}
}
for(Object o:remove){
res.remove(o);
}
for(Object o:res){
System.out.println(o.toString());
}
}
public void compute(ArrayList array,StringBuffer sb){
if(!array.isEmpty()){
for(int i=0;i<array.size();i++){
sb.append(array.get(i));
ArrayList array2=(ArrayList)array.clone();
array2.remove(i);
compute(array2,sb);
sb.delete(sb.length()-1,sb.length());
}
}else{
res.add(sb.toString());
}
}
public ArrayList getArray(){
return res;
}
}
import java.io.BufferedWriter;
import java.io.FileWriter;public class Test {
public static void main(String[] args) throws Exception {
BufferedWriter out = new BufferedWriter(new FileWriter("result.txt"));
String separator = System.getProperty("line.separator");
int count = 0;
for (int i = 122345; i <= 543221; i++) {
String s = "" + i;
if (s.indexOf("4") == 2) {
continue;
} else if (s.indexOf("35") >= 0 || s.indexOf("53") >= 0) {
continue;
} else {
String aaa = "122345";
boolean exist = true;
for (int j = 0; j < s.length(); j++) {
char a = s.charAt(j);
int index = aaa.lastIndexOf(a);
if (index == -1) {
exist = false;
break;
} else {
aaa = aaa.substring(0, index) + aaa.substring(index + 1);
}
}
if (!exist) {
continue;
}
count++;
out.write(s + separator);
}
}
System.out.println("find : " + count);
out.close();
}
}
利用java 中的集合 象 arriylist 等 先把着6个数字 放到集合中 然后进行混排,
然后依次取出,下来再进行判断 是否满足条件 不满足的话 返回继续循环 满足的话输出 返回继续循环 。
大家看下着样行吗 ?
import java.util.regex.Pattern;
import java.util.regex.Matcher;public class Test {
public static void main(String[] args) throws Exception {
FileWriter out = new FileWriter("result.txt");
for (int i = 122345; i <= 543221; i++) {
String s = String.valueOf(i);
if (s.indexOf("35") >= 0 || s.indexOf("53") >= 0) {
continue;
} else{
//必须是1,2,3,4,5之间的数字
Pattern p = Pattern.compile("^[1-5]{6}$");
Matcher m = p.matcher(s);
boolean b = m.matches();
if (b) {
//第三位不能为4
Pattern pat = Pattern.compile("^[1-5]{2}[^4][1-5]{3}$");
Matcher mat = pat.matcher(s);
boolean bool = mat.matches();
if (bool) {
s = s + "\r\n";
out.write(s, 0, s.length());
}
}
}
}
out.close();
}
}
for(int i=122345;i<=543221;i++){
String temp=String.valueOf(i);
if(temp.indexOf("1")+temp.indexOf("2")+temp.lastIndexOf("2")+temp.indexOf("3")+temp.indexOf("4")+temp.indexOf("5")==0+1+2+3+4+5
&&temp.indexOf("1")>=0&&temp.lastIndexOf("2")>=0&&temp.indexOf("2")>=0&&temp.indexOf("3")>=0&&temp.indexOf("4")>=0&&temp.indexOf("5")>=0){
if(temp.charAt(2)!='4'&&temp.indexOf("35")==-1&&temp.indexOf("53")==-1)
System.out.println(temp);
}
}