2011-06-24 27 views
3

我正在研究一個涉及「動態規劃」的項目,並對這個小問題有所瞭解,請大家幫忙。Java中一個數字的所有二進制組合列表

想我拿4作爲輸入,我要顯示這樣的:0000〜1111

但是,如果我輸入5,我要顯示這樣的:00000到11111等等。

由於提前,

編輯:請不要張貼問我的代碼。這不是一個家庭作業問題,我不需要任何代碼,只要告訴我它的邏輯,我會很高興。

EDIT2:WTH與Stackoverflow一起發生,我問你們任何人爲我寫代碼嗎?我想要那個沮喪的人加入它。如果我無法尋求幫助,這個論壇的重點是什麼?

與我分享邏輯。我們可以討論,我不需要這樣的代碼。

EDIT3:在這裏,我發佈了我試過的代碼。我希望這個「滿意」的所有人都認爲我沒有嘗試過任何東西。

import java.util.ArrayList; 

公共類RegularInvestigator {

公共的ArrayList createCombinations(ArrayList的listOfFlightNumbers){

ArrayList<String> result = new ArrayList<String>(); 

for(int i = 1; i < listOfFlightNumbers.size(); i++) { 

    String binaryEqvivalent = Integer.toBinaryString(i);System.out.println(binaryEqvivalent); 
    String element = ""; 

    for(int j = 0; j < binaryEqvivalent.length(); j++) 
    if(binaryEqvivalent.charAt(j) == '1') 
     element += listOfFlightNumbers + " "; 

    result.add(element.substring(0, element.length() - 1)); 
} 

return result; 

}

private String getContent(ArrayList<String> flight) { 
String temp = ""; 

for(int i = 0; i < flight.size() - 1; i++) temp += flight.get(i) + " "; 

temp += flight.get(flight.size() - 1); 

return temp; 

}

私人ArrayList的removeElementAtIndex(ArrayList的飛行,INT位置){

ArrayList<String> res = new ArrayList<String>(); 

for(int i = 0; i < flight.size(); i++) { 
    if(i != position) res.add(flight.get(i)); 
} 

return res; 

}}

EDIT4:謝謝phoxis,PengOne,傑裏棺材oliholz您的寶貴答案:)

+0

你需要顯示所有組合從0000到1111或僅僅指剛0000和1111 – gmhk

+3

共享代碼,以及檢查它 – gmhk

+0

@harigm,從0000到1111的所有組合輸入爲4 – Shankar

回答

8
  • 獲取輸入n
  • i=0計數到(2^n) - 1
  • 爲每個值i位掩碼i的每個位和顯示。
+0

謝謝,但我需要在二進制流的位數是n – Shankar

+0

的值,並通過上面你會得到。只是使用'&'操作符來掩蓋和打印從LSB – phoxis

+2

開始的'n'位?掩碼= 0x01和i =計數,並在每次迭代時留下移位掩碼,並且如果結果爲真,則執行掩碼&i然後打印1 else print 0 – phoxis

1

我對你如何應用動態編程有點遺憾。這只是從0到小於指定的最大值(其中最大值是1左移了指定的位數)的一個數。

編輯:我應該補充說還有其他可能性(例如灰色代碼),但由於沒有其他原因做其他事情,簡單的二進制計數可能是最簡單的實現。

+1

問題說:「我正在處理涉及這個邏輯的問題」,注意「涉及」 – phoxis

+0

@phoxis問題有動態 - 編程標籤。如果動態規劃與問題無關,添加標籤並提及它只會混淆事項。 –

+0

問題標籤無效,我同意,並且一定會令人困惑。 – phoxis

8
​​

帶前導零的類似的東西

for (int i = 0; i < Math.pow(2, value); i++) { 
     StringBuilder binary = new StringBuilder(Integer.toBinaryString(i)); 
     for(int j = binary.length(); j < value; j++) { 
      binary.insert(0, '0'); 
     } 
     System.out.println(binary); 
    } 
+0

Integer.toBinaryString(i)的大小是否爲「value」位? – Shankar

+0

+1簡潔。這將0打印0而不是0000. @asker應該添加填充零的邏輯。 – Kal

+0

感謝您的時間和精力:) – Shankar

4

要麼使用phoxis的非常好的解決方案,或者只是重複他們字典序(這真的是相同的解決方案!):給定一個給定長度的二進制字符串,通過找到最右邊的零條目獲取下一個字典串,將其更改爲1,並將其右側的所有內容更改回0,例如

0 0 0 
0 0 1 
0 1 0 
0 1 1 
1 0 0 
1 0 1 
1 1 0 
1 1 1 
0
int x = 5; 

for(int i = 0; i < (1 << x); i++){ 
System.out.println(Integer.toBinaryString(i)); 
} 
+2

你應該包括你的答案的解釋。 – unclemeat

0

這裏是代碼是找到組合

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package rotateimage; 

/** 
* 
* @author ANGEL 
*/ 
public class BinaryPermutaion { 

    public static void main(String[] args) { 
     //object creation 
     BinaryPermutaion binaryDigit=new BinaryPermutaion(); 
     //Recursive call of the function to print the binary string combinations 
     binaryDigit.printBinary("", 4); 
    } 

    /** 
    * 
    * @param soFar String to be printed 
    * @param iterations number of combinations 
    */ 
    public void printBinary(String soFar, int iterations) { 
    if(iterations == 0) { 
     System.out.println(soFar); 
    } 
    else { 
     printBinary(soFar + "0", iterations - 1); 
     printBinary(soFar + "1", iterations - 1); 
    } 
} 
}