Java經典演算法集——如下:用1、2、2、3、4、5這六個數字,用java寫一個main函式,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"與"5"不能相連。
阿新 • • 發佈:2019-01-02
轉:http://www.blogjava.net/SongJunke/articles/101741.html
演算法程式題:該公司筆試題就1個,要求在10分鐘內作完。
題目如下:用1、2、2、3、4、5這六個數字,用java寫一個main函式,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"與"5"不能相連。
static int[] bits = new int[] { 1, 2, 3, 4, 5 };
/**
* @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;
}
**********************************************************************
基本思路:
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;
}
}
posted on 2007-03-04 14:17 Bill111 閱讀(5920) 評論(5) 編輯 收藏
評論
# re: Java經典演算法集 2007-03-25 08:27 圓規
大家好好看看吧!加油哦!
回覆 更多評論
# re: Java經典演算法集 2008-09-06 10:45 123
好啊好啊
回覆 更多評論
# re: Java經典演算法集 2008-10-22 20:11 cc-yuechuzhao
我的演算法,不用任何資料結構
public class choose6 {
public static void main(String[] args) {
int t=0;
int x;
for (x = 122345; x < 543222; x++) {
if ((x + "").indexOf('2') >= 0) {
if ((x + "").indexOf('2', (x + "").indexOf('2')) >= 0) {
if (((x + "").indexOf('3') >= 0)
&& ((x + "").indexOf('4') >= 0)
&& ((x + "").indexOf('5') >= 0)
&&((x + "").indexOf("35")<0)
&&((x + "").indexOf("53")<0)
&&((x + "").indexOf('4')!=2)
)
++t;
System.out.println(x);
}
}
}System.out.println(t);
}
}
回覆 更多評論
# re: Java經典演算法集 2009-11-10 15:10 感應
@cc-yuechuzhao
錯的
回覆 更多評論
# re: Java經典演算法集 2010-07-02 15:01 tt
public static void main(String[] args) {
for(int i=111111;i<555555;i++){
String str=i+"";
if(str.indexOf("35")==-1 && str.indexOf("53")==-1){
if(!str.substring(2).startsWith("4")){
System.out.println(str);
}
}
}
}