A shuffling algorithm is provided and the cards are stored in an integer array.

Source: Internet
Author: User

A shuffling algorithm is provided and the cards are stored in an integer array. (IDEA 1)

 


The solution to this question is: store 1 to 54 cards randomly in an array of 54 sizes. The time and space complexity of this method are relatively large.

 


This article uses the rand method in <stdlib. h> to generate a random number. During the debugging process, it is found that the random number after each running is the same. The solution is as follows:

First, two functions are provided.

Function 1: int rand (void );
Returns a random integer between [seed, RAND_MAX (0x7fff)] starting from the seed specified in srand (seed.
Function 2: void srand (unsigned seed );
The seed parameter is the seed of rand () and used to initialize the starting value of rand.


Therefore, we can think that when rand () is called every time, it will view:
1) If you have previously called srand (seed) and specified a value for seed, it will automatically call srand (seed) once to initialize its starting value.
2) If you have not called srand (seed) before, it will automatically call srand (1) once.


Based on the first point above, we can conclude that:
1) If you want rand () to generate different values each time the program runs, you must change the value of seed in srand (seed, this variable value must be different each time the program runs (for example, the time elapsed so far ).
2) Otherwise, if you specify a value for seed, the value generated by rand () will be the same each time the program runs, although the value will be [seed, RAND_MAX (0x7fff).
3) If srand (seed) is not called before calling rand (), the effect will be the same as calling rand (1) and then calling rand () (1 is also a set value ).

 

 

In the code, add srand (time (0); to solve this problem.


[Cpp]
Srand (time (0); // the random number generated must be different each time.
For (I = 0; I <CARDS_NUM; I ++)
{
RandPosition = rand () % 54;
InsertCard (randCards, randPosition, cards [I]);
}

Srand (time (0); // the random number generated must be different each time.
For (I = 0; I <CARDS_NUM; I ++)
{
RandPosition = rand () % 54;
InsertCard (randCards, randPosition, cards [I]);
}

About time_t time (0 ):

Time_t is defined as a long integer. It returns the time elapsed since 00:00:00, January 1, January 1, 1970, in seconds.

 

[Cpp]
// Give an algorithm for shuffling and store the cards in an integer array.
 
// A deck of 54 cards, 1--13 indicating the black Peach; 14--26 indicating the red peach;
// 27--39 indicates plum blossom; 40--52 indicates Square; 51 Xiaowang; 52 dawang.
 
 
# Include <stdio. h>
# Include <stdlib. h>
# Include <time. h>
 
# Define CARDS_NUM 54
 
// Create a deck (create an integer array of 1 to 54)
Int * createCards (int * cards)
{
Int I = 0;
For (I = 0; I <CARDS_NUM; I ++)
{
* Cards = I + 1;
Cards ++;
}
Return cards;
}
 
// Print the elements in the array
Void printCards (int * cards)
{
Int flag = 0;
Int I = 0;
For (I = 0; I <CARDS_NUM; I ++)
{
If (flag = 13)
{
Printf ("\ n ");
Flag = 1;
}
Else
{
Flag ++;
}
Printf ("% 3d", * cards );
Cards ++;
}
Printf ("\ n ");
}
 
// Locate the vacancy in an array from the current position (Cyclic scan)
Int findFreePosition (int * cards, int currentPosition)
{
Int p = currentPosition;
While (* (cards + p )! = 0)
{
P = (p + 1) % CARDS_NUM;
}
Return p;
}
 
// Insert a card to a problem in the array
Int * insertCard (int * cards, int currentPosition, int card)
{
Int p = 0;
P = findFreePosition (cards, currentPosition );
* (Cards + p) = card;
Return cards;
}
 
Void main ()
{
Int I = 0;
Int randPosition = 0;
Int cards [CARDS_NUM] = {0}; // sequential card
Int randCards [CARDS_NUM] = {0}; // unordered card
 
Printf ("a deck of 54 cards: 1--13 indicates the black Peach; 14--26 indicates the red peach; \ n ");
Printf ("27--39 indicates plum blossom; 40--52 indicates Square; \ n ");
Printf ("51 indicates the king; 52 indicates the king. \ N ");
 
CreateCards (cards );
Printf ("original order card: \ n ");
PrintCards (cards );
 
Srand (time (0); // the random number generated must be different each time.
For (I = 0; I <CARDS_NUM; I ++)
{
RandPosition = rand () % 54;
InsertCard (randCards, randPosition, cards [I]);
}
Printf ("\ n: \ n" after random shuffling ");
PrintCards (randCards );
}

// Give an algorithm for shuffling and store the cards in an integer array.

// A deck of 54 cards, 1--13 indicating the black Peach; 14--26 indicating the red peach;
// 27--39 indicates plum blossom; 40--52 indicates Square; 51 Xiaowang; 52 dawang.


# Include <stdio. h>
# Include <stdlib. h>
# Include <time. h>

# Define CARDS_NUM 54

// Create a deck (create an integer array of 1 to 54)
Int * createCards (int * cards)
{
Int I = 0;
For (I = 0; I <CARDS_NUM; I ++)
{
* Cards = I + 1;
Cards ++;
}
Return cards;
}

// Print the elements in the array
Void printCards (int * cards)
{
Int flag = 0;
Int I = 0;
For (I = 0; I <CARDS_NUM; I ++)
{
If (flag = 13)
{
Printf ("\ n ");
Flag = 1;
}
Else
{
Flag ++;
}
Printf ("% 3d", * cards );
Cards ++;
}
Printf ("\ n ");
}

// Locate the vacancy in an array from the current position (Cyclic scan)
Int findFreePosition (int * cards, int currentPosition)
{
Int p = currentPosition;
While (* (cards + p )! = 0)
{
P = (p + 1) % CARDS_NUM;
}
Return p;
}

// Insert a card to a problem in the array
Int * insertCard (int * cards, int currentPosition, int card)
{
Int p = 0;
P = findFreePosition (cards, currentPosition );
* (Cards + p) = card;
Return cards;
}

Void main ()
{
Int I = 0;
Int randPosition = 0;
Int cards [CARDS_NUM] = {0}; // sequential card
Int randCards [CARDS_NUM] = {0}; // unordered card

Printf ("a deck of 54 cards: 1--13 indicates the black Peach; 14--26 indicates the red peach; \ n ");
Printf ("27--39 indicates plum blossom; 40--52 indicates Square; \ n ");
Printf ("51 indicates the king; 52 indicates the king. \ N ");

CreateCards (cards );
Printf ("original order card: \ n ");
PrintCards (cards );

Srand (time (0); // the random number generated must be different each time.
For (I = 0; I <CARDS_NUM; I ++)
{
RandPosition = rand () % 54;
InsertCard (randCards, randPosition, cards [I]);
}
Printf ("\ n: \ n" after random shuffling ");
PrintCards (randCards );
}

 

First shuffling:

 

 
 

 

The second shuffling:

 


 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.