# include # include using namespace std; int main ( ) // // POKER_HANDS2 tries to compute the number of possible hands in // poker. The correct answer is 52-choose-5 or 52!/(47!*5!). // However, it is dangerous to try to compute the factorials // first, since 52!, for instance, is a number with at last 40 digits, // while the largest legal integer has 10 digits. // // So we replace the CHOOSE function used in POKER_HANDS by a // more careful computation called CHOOSE2. // { int choose2 ( int n, int k ); int n, k, hands; n = 52; k = 5; hands = choose2 ( n, k ); cout << "From a deck of " << n << " cards," << " we choose " << k << " cards to form a hand.\n"; cout << "Assuming the order of the 5 cards doesn't matter,\n"; cout << "there are " << hands << " different hands we can draw.\n"; return 0; } int choose2 ( int n, int k ) // // CHOOSE2 computes N! / (K!*( N-K!)) by // // value = 1 * N/1 * (N-1)/2 * (N-2)/3 * ... (N-K+1)/K. // // This result avoids unnecessary overflow caused by evaluating // factorials. // { int bot; int i; int top; int value; value = 1; top = n; bot = 1; for ( i = 1; i <= k; i++ ) { value = ( value * top ) / bot; top = top - 1; bot = bot + 1; } return value; }