*
* ÇómÈ¡nµÄËùÓÐ×éºÏ¡£
* m¸öÊý·Ö±ðΪ0,1,2...m-1.
* Ëã·¨¼òÊö£º
* ¶þ¸ö×éºÏ£¬Èô½öÓÐÔªËØ˳Ðò²»Í¬£¬ÊÓÆäΪͬһ¸ö×éºÏ¡£
* ×óλϵµÍ룬ÓÒλϵ¸ßλ¡£
* °´×ÔÈ»µÄÈ¡·¨È¡µÚÒ»¸ö×éºÏ(¸÷Êýλ·Ö±ðÊÇ:0,1,2...n-1)£¬ÒÔºóµÄËùÓÐ×éºÏ¶¼¾ÉÏÒ»¸ö×éºÏ±ä»¯¶øÀ´:
* ´ÓÓÒÖÁ×ó£¬ÕÒµ½ÓÐÔöÁ¿¿Õ¼äµÄλ,½«Æä¼Ó1,ʹ¸ßÓÚ¸ÃλµÄËùÓÐ룬¾ù±ÈÆä×óÁÚλ´ó1£¬´Ó¶øÐγÉеÄ×éºÏ¡£
* ÈôËùÓÐλ¾ùÎÞÔöÁ¿¿Õ¼ä£¬ËµÃ÷ËùÓÐ×éºÏ¾ùÒѱ»±éÀú¡£
* ʹÓø÷½·¨ËùÉú³ÉµÄ×éºÏÊýÖУº¶ÔÈÎÒâ×éºÏint[] c,ϱêСµÄÊý±Ø¶¨Ð¡ÓÚϱê´óµÄÊý.
*
*/
public class Combination {
int n, m;
int[] pre;//previous combination.
public Combination(int n, int m) {
this.n = n;
this.m = m;
}
/**
* È¡ÏÂÒ»¸ö×éºÏ¡£¿É±ÜÃâÒ»´ÎÐÔ·µ»ØËùÓеÄ×éºÏ(ÊýÁ¿¾Þ´ó£¬ÀË·Ñ×ÊÔ´)¡£
* if return null,ËùÓÐ×éºÏ¾ùÒÑÈ¡Íê¡£
*/
public int[] next() {
if (pre == null) {//È¡µÚÒ»¸ö×éºÏ£¬ÒÔºóµÄËùÓÐ×éºÏ¶¼¾ÉÏÒ»¸ö×éºÏ±ä»¯¶øÀ´¡£
pre = new int[n];
for (int i = 0; i < pre.length; i++) {
pre[i] = i;
}
int[] ret = new int[n];
System.arraycopy(pre, 0, ret, 0, n);
return ret;
}
int ni = n - 1, maxNi = m - 1;
while (pre[ni] + 1 > maxNi) {//´ÓÓÒÖÁ×ó£¬ÕÒµ½ÓÐÔöÁ¿¿Õ¼äµÄλ¡£
ni--;
maxNi--;
if (ni < 0)
return null;//ÈôδÕÒµ½£¬ËµÃ÷ÁËËùÓеÄ×éºÏ¾ùÒÑÈ¡Íê¡£
}
pre[ni]++;
while (++ni < n) {
pre[ni] = pre[ni - 1] + 1;
}
int[] ret = new int[n];
System.arraycopy(pre, 0, ret, 0, n);
return ret;
}
}
Ã÷ÌìÔÙ˵ ÓÚ 2009-11-10 21:58:34·¢±í:
ÎҵúúÃѧϰÁË
zhqs ÓÚ 2006-10-30 10:53:02·¢±í:
ºÃºÃѧϰѧϰ!