ygm224
小水手

UID 30271
精华
0
积分 1
帖子 1
阅读权限 10
注册 2007-6-25
状态 离线
|
有道E文题,想请教高手
Introduction
Supposed you have a sorted deck of cards sorted by suit (spades, diamonds, hearts, clubs) and then number (ace to king):
AS 2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD AH 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC
To randomize the deck, one can shuffle the cards. We can simulate this shuffle physically using the follow procedure:
1. Split the deck exactly in half, forming two half decks (26 cards each).
2. Start with an empty “new” deck.
3. Select a card from the “top” of one of the half decks. Selection of which deck is done randomly such that there is a 50% chance to select either deck (unless of course one of the half decks is empty). Place the selected card at the bottom of the new deck.
4. Repeat step #3 until all the cards are in the new deck.
This process is illustrated for the first few steps here:
Step #1 and #2
half deck #1
AS 2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD half deck #2
AH 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC
new deck
[empty]
Step #3.1
Randomly select half deck #1.
half deck #1
2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD half deck #2
AH 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC
new deck
AS
Step #3.2
Randomly select half deck #2.
half deck #1
2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD half deck #2
2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC
new deck
AS AH
Step #3.3
Randomly select half deck #2.
half deck #1
2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD half deck #2
3H 4H 5H 6H 7H 8H 9H TH JH QH KH AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC
new deck
AS AH 2H
With a single shuffle, it is quite apparent that the ace of spades will likely be near the top of the new deck. A single shuffle does not randomize things very well! To better randomize the deck, the new deck undergoes this shuffle procedure a number of times. In this assignment we will assess the randomness by examine the position of the ace of spades as a function of the number of shuffles.
Problem
Write a program that will accept a user input for the number of trials and the number of shuffles per trial. For each trial, the program will begin with a sorted deck of cards and perform the specified number of shuffles. The position of the ace of spades will be determined for each trial. After all the trials, the program will report a summary of the positions of the ace of spades across all the trials. Positions are separated into 13 bins and the percentage of trials that occur in each bin is reported. Ensure that the summary information is presented in a format that conforms to the example shown below.
Note: You should see with 10 shuffles that the position of the ace of spades is quite well randomized.
In the program you should use the structure:
struct Card {
// number varies from 1 to 13 (1=ACE, 11=JACK, 12=QUEEN, 13=KING)
int number;
char suit; // 'S'=SPADES 'D'=DIAMONDS 'H'=HEARTS 'C'=CLUBS
};
and write the function (you should write additional functions as you feel necessary):
/*-------------------------------------------------------------------------
void shuffle(Card deck[NCARD])
This function performs a single shuffle.
1. Split the deck exactly in half, forming two half decks (26 cards each).
2. Start with an empty "new" deck.
3. Select a card from the "top" of one of the half decks. Selection of
which deck is done randomly such that there is a 50% chance to select
either deck (unless of course one of the half decks is empty). Place
the selected card at the bottom of the new deck.
Inputs
deck: the input deck of cards
Outputs
deck: the output deck of cards (after a single shuffle)
Modifications
07/04/10 AC First created.
-------------------------------------------------------------------------*/
Example output
Enter the number of trials to perform: 2
Enter the number of shuffles to perform: 3
DEBUG OUTPUTS (y/n): n
Bin 1 (Position 1 to 4): 50.00%
Bin 2 (Position 5 to 8): 0.00%
Bin 3 (Position 9 to 12): 0.00%
Bin 4 (Position 13 to 16): 0.00%
Bin 5 (Position 17 to 20): 0.00%
Bin 6 (Position 21 to 24): 0.00%
Bin 7 (Position 25 to 28): 0.00%
Bin 8 (Position 29 to 32): 0.00%
Bin 9 (Position 33 to 36): 0.00%
Bin 10 (Position 37 to 40): 50.00%
Bin 11 (Position 41 to 44): 0.00%
Bin 12 (Position 45 to 48): 0.00%
Bin 13 (Position 49 to 52): 0.00%
Press any key to continue
Example output (with debugging outputs shown)
Enter the number of trials to perform: 2
Enter the number of shuffles to perform: 3
DEBUG OUTPUTS (y/n): y
Trial #1
Initialize deck
AS 2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD AH
2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC
Shuffle #1
AH 2H 3H AS 4H 2S 5H 6H 3S 4S 5S 6S 7S 8S 9S TS 7H JS QS KS AD 2D 3D 4D 8H 9H 5D
6D 7D 8D TH JH QH 9D TD KH AC 2C 3C JD 4C 5C QD 6C 7C KD 8C 9C TC JC QC KC
Shuffle #2
5D AH 6D 2H 7D 8D TH JH QH 9D 3H TD KH AS AC 2C 3C JD 4H 4C 2S 5H 5C QD 6C 7C 6H
KD 3S 4S 8C 9C TC 5S JC QC KC 6S 7S 8S 9S TS 7H JS QS KS AD 2D 3D 4D 8H 9H
Shuffle #3
6H KD 5D 3S AH 4S 8C 9C TC 6D 5S 2H JC QC KC 7D 6S 8D TH 7S 8S 9S TS JH QH 9D 3H
TD 7H JS KH QS KS AD 2D 3D 4D 8H 9H AS AC 2C 3C JD 4H 4C 2S 5H 5C QD 6C 7C
Position of ace of spades is: 40
Trial #2
Initialize deck
AS 2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD AH
2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC
Shuffle #1
AH AS 2H 2S 3H 3S 4H 5H 6H 7H 4S 8H 5S 9H 6S 7S TH JH QH KH 8S AC 2C 3C 4C 5C 6C
9S TS JS QS 7C 8C KS 9C TC JC AD QC KC 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD
Shuffle #2
AH 6C AS 2H 9S 2S 3H TS 3S JS 4H QS 7C 8C 5H 6H 7H KS 4S 9C TC 8H JC AD QC 5S 9H
6S KC 2D 7S 3D 4D 5D 6D 7D TH JH QH 8D 9D TD KH JD 8S QD KD AC 2C 3C 4C 5C
Shuffle #3
AH 6C AS 9H 2H 9S 6S KC 2D 2S 7S 3H TS 3D 4D 3S JS 4H 5D QS 7C 8C 5H 6D 6H 7D 7H
TH JH KS 4S 9C QH 8D TC 8H JC AD 9D QC TD KH JD 8S QD 5S KD AC 2C 3C 4C 5C
Position of ace of spades is: 3
Bin 1 (Position 1 to 4): 50.00%
Bin 2 (Position 5 to 8): 0.00%
Bin 3 (Position 9 to 12): 0.00%
Bin 4 (Position 13 to 16): 0.00%
Bin 5 (Position 17 to 20): 0.00%
Bin 6 (Position 21 to 24): 0.00%
Bin 7 (Position 25 to 28): 0.00%
Bin 8 (Position 29 to 32): 0.00%
Bin 9 (Position 33 to 36): 0.00%
Bin 10 (Position 37 to 40): 50.00%
Bin 11 (Position 41 to 44): 0.00%
Bin 12 (Position 45 to 48): 0.00%
Bin 13 (Position 49 to 52): 0.00%
Press any key to continue
|
|