import java.util.Random;public class BinaryCompare {
/**
* 得到在一个指定的数standNum范围中的length个的数字
* @param standNum
* @param length
* @return
*/
public int[] getIntList(int standNum,int length) {
Random r = new Random();
int x[] = new int[length];
for (int i = 0; i < length; i++) {
x[i] = r.nextInt(standNum);
}
return x;
}
/**
* 计算一个数的2进位位数,比如3为11(2位),5为101(3位),6为110(3位)
* 0为0(1位)
* @param num
* @return
*/
public int getBinaryNum(int num) {
int i = 0;
if (num == 0) {
return 1;
}
while (!(num < Math.pow(2,i+1) && num >= Math.pow(2,i))) {
i++;
}
return i+1;
}
/**
* 找到在一个指定的数standNum范围中的length数目的数字中的
* 最多2个位相同的列表对(i,j)
* @param standNum
* @param length
*/
public void getMaxTwoDisNumList(int standNum,int length) {
int binaryBitNums = getBinaryNum(standNum);
int numbers[] = getIntList(standNum,length);
for (int i = 0; i < length-1; i++) {
for (int j = i+1; j < length; j++) {
int temp = numbers[i] ^ numbers[j];
//System.out.println(Integer.toBinaryString(temp));
int k = 0;
boolean isRight = true;
for ( int m = 0 ; m < binaryBitNums; m++) {
if(temp >= 1) {
if (temp % 2 == 1) {
k++;
}
temp = temp/2;
}
if (k > 2) {
isRight = false;
break;
}
}
if ((i != j) && isRight) {
System.out.println("RightNumPARTON:");
System.out.println("x["+i+"]"+Integer.toBinaryString(numbers[i]));
System.out.println("x["+j+"]"+Integer.toBinaryString(numbers[j]));
} }
}
}
public static void main(String args[]) {
BinaryCompare b = new BinaryCompare();
int num2x10 = (int) (Math.pow(2,10)-1);
b.getMaxTwoDisNumList(num2x10,100);
}}
/**
* 得到在一个指定的数standNum范围中的length个的数字
* @param standNum
* @param length
* @return
*/
public int[] getIntList(int standNum,int length) {
Random r = new Random();
int x[] = new int[length];
for (int i = 0; i < length; i++) {
x[i] = r.nextInt(standNum);
}
return x;
}
/**
* 计算一个数的2进位位数,比如3为11(2位),5为101(3位),6为110(3位)
* 0为0(1位)
* @param num
* @return
*/
public int getBinaryNum(int num) {
int i = 0;
if (num == 0) {
return 1;
}
while (!(num < Math.pow(2,i+1) && num >= Math.pow(2,i))) {
i++;
}
return i+1;
}
/**
* 找到在一个指定的数standNum范围中的length数目的数字中的
* 最多2个位相同的列表对(i,j)
* @param standNum
* @param length
*/
public void getMaxTwoDisNumList(int standNum,int length) {
int binaryBitNums = getBinaryNum(standNum);
int numbers[] = getIntList(standNum,length);
for (int i = 0; i < length-1; i++) {
for (int j = i+1; j < length; j++) {
int temp = numbers[i] ^ numbers[j];
//System.out.println(Integer.toBinaryString(temp));
int k = 0;
boolean isRight = true;
for ( int m = 0 ; m < binaryBitNums; m++) {
if(temp >= 1) {
if (temp % 2 == 1) {
k++;
}
temp = temp/2;
}
if (k > 2) {
isRight = false;
break;
}
}
if ((i != j) && isRight) {
System.out.println("RightNumPARTON:");
System.out.println("x["+i+"]"+Integer.toBinaryString(numbers[i]));
System.out.println("x["+j+"]"+Integer.toBinaryString(numbers[j]));
} }
}
}
public static void main(String args[]) {
BinaryCompare b = new BinaryCompare();
int num2x10 = (int) (Math.pow(2,10)-1);
b.getMaxTwoDisNumList(num2x10,100);
}}
http://community.csdn.net/Expert/topic/4501/4501607.xml?temp=.1983148