Method 1
Commonly used data simulation and construction methods for solving practical problems by computers. With arrays, data simulation and construction can be easily implemented.
The idea of randomly generating playing cards:
1. Define the character array to construct a new card that is stored in sequence, including the color and card value.
2. disrupt New cards based on random numbers.
3. distribute new cards by Array (displayed according to rules ).
The Code is as follows:
# Include <stdio. h>
# Include <stdlib. h>
Void main (void)
{
Int I, j;
Char success, ch2;
Char s [104];/* store a deck */
Char a [] = "hdsc";/* define the color */
/* Define the card value */
Char B [14] = "akqj1098765432 ";
/* Generate a new card */
For (I = 0; I <4; I ++)
{
For (j = 0; j <13; j ++)
{
S [2 * j + 26 * I] = a [I];
S [2 * j + 26 * I + 1] = B [j];
}
}
Randomize ();/* initialize the random number generator */
For (I = 0; I <52; I ++)
{
J = random (52-i );
Category = s [2 * j];/* Exchange colors with the last card */
S [2 * j] = s [102-2 * I];
S [102-2 * I] = bytes;
Ch2 = s [2 * j + 1];/* Exchange card value with the last card */
S [2 x j + 1] = s [103-2 x I];
S [103-2 * I] = ch2;
}
Printf ("nn ");
For (I = 0, j = 0; I <104; I + = 2, j ++)/* display new cards after chaos */
{
If (! (J % 13 ))
{
Printf ("nnn");/* 13 cards in a group */
}
Printf ("% c", s [I], s [I + 1]);
}
Getch ();
}
Method 2
# Include <stdlib. h>
# Include <string. h>
# Include <time. h>
# Include <iostream>
// Using namespace std;
Int main ()
{
Const char * c = "0123456789 abcdefghijklmnopqrstuvwxyz ";
Srand (static_cast <unsigned int> (time (null )));
Std: cout <"the random char array is :";
For (int I = 0; I <5; ++ I)
{
Std: cout <std: endl;
Int index = 0;
For (int j = 0; j <5; ++ j)
{
Index = rand () % strlen (c );
Std: cout <c [index]
<C [index]
<"";
}
}
Std: cout <std: endl;
Return 0;
}
The running effect is as follows:
The random char array is:
00 qq 88 ii zz
Qq bb oo 00 yy
Rr yy tt ww ll
Ss rr aa bb oo
11 yy tt 33 uu