Chapter 1 array
The first array is the most common bulk data storage structure. Since the array is stored in order, random storage can be conveniently stored. In many languages, arrays are static, and must specify the length of the array before use, and in PHP, you don't have to worry about this problem, the array is completely dynamic. You can even use an array like this. PHP Code
// demo of array demo_Array [] = 1; demo_Array [] = 2;
Below we use an array to simulate a sports-mahjong shuffle with the vast number of people like China. Here, I will popularize the rules of mahjong, and it is useful when serving MM's mother :). First of all, mahjong is 4 people to play, 3P is not. A total of 108 cards, touched 14 households, 13 people per person, 55 left. (Mahjong has n kinds of gameplay, here is the gameplay of the Easy hometown, there is no Chinese style, which is the simplest gameplay in Easy.) Then we come to analyze how to simulate this with random Process, this exercise may wish to do more, because the mathematical model is extracted from a specific case is the basic skill of programming. First, we use a group with a length of 108 to store these 108 cards. When you initialize it, it is possible to assume that they are emissions. Then, let's simulate the process of shuffling, which is actually a process of several two cards. We generate two random numbers between 1 to 108, and then exchange the value of the array elements that are index that is index. Because of multiple exchanges, we can define this process as a function. Then, it is to give this 108 sheets to 4 people. In fact, as long as the number of exchanges in front is large enough, it is OK directly into 5 copies. However, in order to better simulate reality and performance array, we use a loop way to distribute. Everyone is four times, then one person, two people. Ok, the whole process is like this. Lower We use the program to explain some more detail.
PHP Code
php / ** ****************************************************** *************** * An example of using an array to simulate mahjong shuffling and licensing procedures * @ Easy@bjpeu.edu.cn * @ 2003-6-3 ** * *********************************************************** ********* / / / Function Function SWAP () {// Define $ MajiangArray as global variables // This can be operated in a function to operate Global $ MajiangArray; // Generate two 1 108 random number $ index1 = rand (1, 108); $ index2 = rand (1, 108); // Exchange the value of the corresponding array element $ TMP = $ MAJIANGARRAY [$ index1]; $ massageRay [$ index1] = $ MajiangArray [$ @ ararray [$ index2] = $ TMP;} // will use the contents of the array with the corresponding picture to function Function Showarray ($ accyname) {$ count = count ($ arrayname); for $ I = 0; $ i <$ count; $ i ) {Echo ''; // Display the corresponding picture} Echo '
'; // Railing} // Main program part start // initialization array unset ($ majiangArray); $ majiangArray [] =' 0 '; // data from 1 Start storage, skip 0 for ($ i = 1; $ I <= 3; $ i ) // A total of three colors {for ($ j = 1; $ j <= 9; $ j ) // A color in a flower {for ($ k = 1; $ k) <= 4; $ k ) // There are four {$ MajiangArray [] {$ MajiangArray [] = $ i. '_'. $ J. '.Gif';}}} // Start Wash FOR ($ I = 1 ; $ i <= 1000; $ i The cyclic variable I is reused {swap ();} // initializes four users unset ($ user1); unset ($ user2); unset ($ user4); // $ seek It is the progress pointer, and the record is currently deducted from there. $ Seek = 1; for ($ I = 1; $ I <= 3; $ i ) / / Total three times {for ($ J = 1; $ j) <= 4; $ J ) // Four people {for ($ k = 1; $ k <= 4; $ k ) // Time four {$ name = 'user'. $ J; $ {$ name} [ ] = $ MAJIANGARRAY [$ SEEK ];}}} // Then one person is a for ($ I = 1; $ I <= 4; $ i ) {$ name = 'user'. $ i;
$ {$ Name} [$ seek ];} // Last Dammer Touch a $ user4 [] = $ MajiangArray [$ seek]; // Branch end display result showarray ($ user1); showarray ($ USER2); SHOWARRAY ($ USER3); SHOWARRAY ($ USER4);?> Let's take a look at our results:
Is it very accomplished? what? What is it a little strange? Because no one will play a launch, we always put a group of cards to one side, arrange them in order (I heard people who deliberately put the cards, but that is the ashes of the players ~), so, In a section we will continue to improve this procedure to discuss the sorting problem of arrays.