62,614
社区成员
发帖
与我相关
我的任务
分享
String str="XXXXXXXXXD"
//以此类推 是2(二进制0000000010)的情况
String str ="XXXXXXXXDX"
//推理完成代码如下
String str="";
for (int i = 0; i < Integer.MAX_VALUE; i++) {
str = Integer.valueOf(i).toBinaryString(i);
if (str.length() != 10) {
//这里用了一个apache的包commons-lang-2.5.jar 包含StringUtils
str = StringUtils.leftPad(str, 10, "0");
}
str = str.replaceAll("0", "X");
str = str.replaceAll("1", "D");
System.out.println(str);
}
同理
public class Test001 {
static int _count = 0;
public static void main(String[] args) {
final char[] ARR = new char[] { 'D', 'X' };
output(ARR, 10, "");
System.out.println("count:" + _count);
}
static void output(char[] arr, int n, String str) {
if (n == 0) {
System.out.println(str);
++_count;
return;
}
for (char c : arr) {
output(arr, n - 1, str + c);
}
}
}
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.LinkedList;
public class Test001 {
public static void main(String[] args) {
final String STR = "DXDXDXDXDX";
Collection<String> collection = getAllPermutation(STR, STR.length(), true);
for (String string : collection) {
System.out.println(string);
}
System.out.println("count:" + collection.size());
}
/**
* 返回该字符串字符的排列组合
*
* @param source
* 字符串
* @param count
* 取出排列字符的个数
* @param isUnique
* 是否需要去重,需要去重设置true
* @return 该字符串所有字符的排列组合,无法获取返回null
*/
public static Collection<String> getAllPermutation(String source,
int count, boolean isUnique) {
if (source == null || source.equals("")) {
return null;
}
Collection<String> collection = isUnique ? new LinkedHashSet<String>()
: new LinkedList<String>();
getAllPermutation(source.toCharArray(), count, 0, collection);
return collection;
}
/**
* 返回字符数组的排列组合
*/
static void getAllPermutation(char[] arr, int count, int index,
Collection<String> collection) {
if (index == count) {
collection.add(String.valueOf(Arrays.copyOfRange(arr,
index - count, index)));
return;
}
for (int i = index; i < arr.length; i++) {
swap(arr, i, index);
getAllPermutation(arr, count, index + 1, collection);
swap(arr, i, index);
}
}
/**
* 返回字符数组的组合
*/
static void getCombination(char[] arr, int count, int begin, int index,
char[] tempArr, Collection<String> collection) {
if (index == count) {
collection.add(String.valueOf(tempArr));
return;
}
for (int i = begin; i < arr.length; i++) {
tempArr[index] = arr[i];
getCombination(arr, count, i + 1, index + 1, tempArr, collection);
}
}
static void swap(char array[], int i, int j) {
char tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
List<String>strList=new ArrayList<String>();
StringBuilder sb=new StringBuilder();
String str;
char[]chs={'D','X'};
Random r=new Random();
while(true)
{
for(int j=0;j<10;j++)
{
sb.append(chs[r.nextInt(2)]);
}
str=sb.toString();
sb.delete(0, sb.length());
if(strList.contains(str))
continue;
else strList.add(str);
if(strList.size()==1024)//能计算出所有可能情况的个数
break;
}
for(int i=0;i<strList.size();i++)
System.out.println(strList.get(i));
System.out.println("总共:"+strList.size());
//新手,自己写了一个,能够按照要求输出显示所有可能组合,求点评!!
class Test {
static char[] digits=new char[]{'X','D'};
public static void main(String[] args) {
for (int i=0; i<1024; i++) {
System.out.println(toUnsignedString(i));
}
}
private static String toUnsignedString(int i) {
char[] buf=new char[10];
int charPos=10;
for (int j=0; j<10; j++) {
buf[--charPos]=digits[i&1];
i>>>=1;
}
return new String(buf);
}
}
for (int i = 1024; i < 2048; i++) {
String binStr = Integer.toBinaryString(i);
System.out.println(binStr);
}
这样就可以了把