Меню сайта


Класс «CombinationGenerator»

class CombinationGenerator {int[] a;int n;int r;BigInteger numLeft;BigInteger total;CombinationGenerator(int n, int r) {(r > n) {new IllegalArgumentException();

}(n < 1) {new IllegalArgumentException();

}.n = n;.r = r;= new int[r];nFact = getFactorial(n);rFact = getFactorial(r);nminusrFact = getFactorial(n - r);= nFact.divide(rFact.multiply(nminusrFact));();

}

//------

// Reset

//------void reset() {(int i = 0; i < a.length; i++) {[i] = i;

}= new BigInteger(total.toString());

} // Return number of combinations not yet generated

//------------------------------BigInteger getNumLeft() {numLeft;

}

//-----------------------------

// Are there more combinations?

//-----------------------------boolean hasMore() {numLeft.compareTo(BigInteger.ZERO) == 1;

} // Return total number of combinations

//--------------------------BigInteger getTotal() {total;

}

//------------------

// Compute factorial

//------------------static BigInteger getFactorial(int n) {fact = BigInteger.ONE;(int i = n; i > 1; i--) {= fact.multiply(new BigInteger(Integer.toString(i)));

}fact;

}

//--------------------------------

// Generate next combination (algorith from Rosen p. 286)

//----------------------------------int[] getNext() {(numLeft.equals(total)) {= numLeft.subtract(BigInteger.ONE);a;

}i = r - 1;(a[i] == n - r + i) {-;

}[i] = a[i] + 1;(int j = i + 1; j < r; j++) {[j] = a[i] + j - i;

}= numLeft.subtract(BigInteger.ONE);

return a;

}

}