Пример #1
0
main()
{
    int N = 50;
    int R = 5;
    int *x, *z;
    int n;
    int i;

    n = nCr(N, R);
    printf("%dC%d = %d\n", N, R, n);
/*
    n = nPr(N, R);
    printf("%dP%d = %d\n", N, R, n);
*/

    if ( (x = (int *)malloc( N * sizeof(int) )) == NULL ) {
        printf("*** malloc failed for x\n");
        exit(1);
    }
    for(i=0; i<N; i++) {
        x[i] = i+1;
    }
    if ( (z = (int *)malloc( N * sizeof(int) )) == NULL ) {
        printf("*** malloc failed for z\n");
        exit(1);
    }
    
    combs(x, N, R, z, 0, comb_count, &n);
 /* perms(x, N, R, z, 0, vec_print, NULL); */

}
Пример #2
0
/*---------------------------------------------------------------
 Routine : combs
 Purpose : Generate combinations, r objects from n.
 (objects are integers in array a, length n)
 f is called for each combination.
 The arguments for f are as for vec_print above.

 This routine is recursive.
---------------------------------------------------------------*/
void
  combs(
    int *a,     /* IN  - input array                              */
    int  n,     /* IN  - length of a                              */
    int  r,     /* IN  - length of combinations to take from a    */
    int *z,     /* OUT - array to put combination into            */
    int  zi,    /* IN  - index into z                             */
    void f( int *, int, void * ),   /* ?     function to call for each comb generated */
    void *p)    /* ?     user data for f()                        */
{
    int i;

    if ( r == 0 ) {
        f(z, zi, p);

		/* update progress bar (if required) */
		current_progress += 1.0f;
		while(current_progress > one_increment) {
		    GenerateNumericalProbabilityProgressBarInc();
            current_progress -= one_increment;
		}

        return;
    } else {
        for(i = 0; i < n-r+1 && !GenerateNumericalProbabilityCheckForInterrupt(); i++) {
            z[zi] = a[i];
            combs(a+i+1, n-i-1, r-1, z, zi+1, f, p);
        }
    }
} /* combs */
Пример #3
0
int main(){
    int n,k;
    ll res;
    while(1){
        scanf("%d %d",&n,&k);
        if(!n && !k) break;
        res = combs(n,k);
        printf("%lld\n",res);
    }
    return 0;
}
Пример #4
0
		vector< vector<T> > combinations(vector<T> &v, unsigned int k) {

			unsigned int next = 0;

			if(k > v.size())
				throw k_GreaterThan_n_Exception();

			vector< vector<T> > combs( binomialCoefficient(v.size(), k) );

			vector<bool> binary(v.size());
			fill(binary.end() - k, binary.end(), true);

			do {
			   for(unsigned int i = 0 ; i < v.size() ; i++) {
				   if(binary[i])
					   combs[next].insert(combs[next].end(), v[i]);
			   }
			   next++;
			} while(next_permutation(binary.begin(), binary.end()));

			return combs;
		}