求高效的java组合算法 哪位高手有高效的组合算法啊,给我一个,谢谢大家了,不要递归的算法。可以计算100中取50个元素的所有组合,而且可以全部输出。在此谢谢大家了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 C 50 100 = A 50 100/ A 50 50(or 50!)A:排列C:组合m,n:n个元素中取m个元素 A m n = n*(n-1)*...(n-m+1)C m n = A m n/A m m = n*(n-1)*...(n-m+1)/m! 递归本来是高效的,逻辑简单写难道你想写50个for循环?O(∩_∩)O哈哈~ ///可取重复///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; }} 求struts2 多文件上传进度条demo 正则表达式匹配中文! missing return value为啥报这个错阿? 一个关于JDBC练接数据库的问题 请问如何把ArrayList对象用数据流存入Oracle的blob字段,再取出。 为什么IE能浏览我下载的.class文件,而不能看我用jbuilder生成的.class文件 请教 java List 泛型转换 诚心交朋友! 如何使用DatabaseDom进行数据库的内容的转换?(100分) 怎样将一个整数转化为字符串? ArrayList问题 西北地区java培训机构名单大全征集进行中....
C:组合
m,n:n个元素中取m个元素 A m n = n*(n-1)*...(n-m+1)C m n = A m n/A m m = n*(n-1)*...(n-m+1)/m!
难道你想写50个for循环?
O(∩_∩)O哈哈~
///可取重复
///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;
}
}