62,616
社区成员
发帖
与我相关
我的任务
分享
///可取重复
///amd cpu 2800+,1.61GHz
///取三位141ms,四位14s,再多不敢试了
public static void main(String[] args) {
short[] dd = new short[100];
for (short i = 0; i < 100; i++) {
dd[i] = i;
}
long t = System.currentTimeMillis();
printf(dd, 4);
System.out.flush();
System.out.println(System.currentTimeMillis() - t);
}
/**看成是keys.length进制的数,打印select位数字
*/
static void printf(byte[] keys, int select) {
byte[] num = new byte[select];
int length = keys.length;
int pos;
for (int i = 0; i < select; i++) {//给num赋初值
num[i] = keys[0];
}
int nowpos = select - 1;
for (long i = 1, j, imax = (long) Math.pow(keys.length, select); i < imax; i++) {
if (i % length == 0) {//有进位则更新多位
j = i;
nowpos = select - 1;
while (j % length == 0) {
num[nowpos--] = keys[0];
j /= length;
}
num[nowpos] = (byte) j;
} else {//无进位则更新个位 ++
num[select - 1] = keys[(int) i % length];
}
// System.err.print(i + ":[");
// for (pos = 0; pos < select; pos++) {//打印值
// System.err.print(num[pos] + (pos < select - 1 ? "," : ""));
// }
// System.err.println("]");
}
}
///
///数据结构,这个算不算
public class NewClass1 {
public static void main(String[] args) {
short[] dd = new short[100];
for (short i = 0; i < 100; i++) {
dd[i] = i;
}
Coll c = new Coll(dd, 50);
///从1111开始1000个打印
for (int i = 0; i < 10; i++) {
short[] num = c.get(1111 + i);
int select = num.length;
System.err.print(":[");
for (int pos = 0; pos < select; pos++) {//打印值
System.err.print(num[pos] + (pos < select - 1 ? "," : ""));
}
System.err.println("]");
}
}
}
/**
*/
class Coll {
private short[] keys;
private int select;
public Coll(short[] keys, int select) {
if (keys == null || keys.length < 1 || select < 1 || select >= keys.length) {
throw new java.lang.ArrayIndexOutOfBoundsException("" + select);
}
this.keys = keys;
this.select = select;
// Arrays.sort(this.keys);
}
/**得到总长度*/
public long getSize() {
return (long) Math.pow(keys.length, select);
}
/**取第index个组合*/
public short[] get(long index) {
if (index < 0 || index >= getSize()) {
throw new java.lang.ArrayIndexOutOfBoundsException("" + index);
}
short[] re = new short[select];
for (int i = select - 1; i > -1; i--) {
if (index > keys.length) {
re[i] = keys[(int) (index % keys.length)];
index /= keys.length;
} else if (index > -1) {
re[i] = keys[(int) (index)];
index = -1;
} else {
re[i] = keys[0];
}
}
return re;
}
/**取全部组合*/
public List<short[]> getList() {
List list = new java.util.LinkedList<short[]>();
short[] num = new short[select];
int length = keys.length;
int pos;
for (int i = 0; i < select; i++) {//给num赋初值
num[i] = keys[0];
}
int nowpos = select - 1;
for (long i = 1, j, imax = getSize(); i < imax; i++) {
if (i % length == 0) {//有进位
j = i;
nowpos = select - 1;
while (j % length == 0) {
num[nowpos--] = keys[0];
j /= length;
}
num[nowpos] = (byte) j;
} else {//无进位则个位 ++
num[select - 1] = keys[(int) i % length];
}
short[] buf = new short[select];
System.arraycopy(num, 0, buf, 0, length);
list.add(buf);
}
return list;
}
}