Playing cards (random sorting)

Source: Internet
Author: User

Solution:

Beginners usually think that 1 ~ The Random Number of N is stored in the array. Before storing the generated random number into the array, you must first check whether there are repeated numbers in the array. If this number exists, generate the next number. If you are not lucky, there will be a lot of repetition times, and the execution speed of the program will be slow. This is not a good solution.

 

Take 1 ~ For example, you can enter the array in ascending order from 1 to 52, then use a loop to access the array, and randomly generate 1 ~ The Random Number of 52 will be used as an index to retrieve the array value and exchange it with the value accessed by the current array, so there is no need to worry about repeated random numbers, after accessing the array, all the numbers will be rearranged.

 

Taking playing cards as an example, how can we determine the color? This is a division problem. Determine the color of the commodity number and the remainder number.

 

 

I wrote it by myself. At the beginning, I planned to write an example of a disordered sorting with strong applicability, but I had to take into account the characteristics of playing cards. Therefore, the name of a function is not very reasonable in parameter design.

 

/** Content: Playing cards (random sorting) * Time: 2/13/2013 */# include <stdio. h> # include <stdlib. h> # include <time. h> # define Max 52 void initnumber (INT array [], int arraylength) // initialize the Array {for (INT I = 1; I <arraylength; ++ I) {array [I] = I ;}} void randomsort (INT array [], int minvalue, int maxvalue) // sort the array in random order {srand (time (0); int temp = 0; int num = 0; For (INT I = 1; I <maxvalue; ++ I) {num = rand () % (maxvalue-minvalue + 1) + minvalue; // generate a random number for this range temp = array [I]; array [I] = array [num]; array [num] = temp;} void printresult (INT array [], int arraylength) {int op = 0; // used to store the color int number = 0; // used to store numbers for (INT I = 1; I <arraylength; ++ I) {op = (array [I]-1)/13; // determine the color switch (OP) {Case 0: printf ("\ t red heart"); break; case 1: printf ("\ t "); break; Case 2: printf ("\ t plum blossom"); break; Case 3: printf ("\ t black peach "); break; default: break;} number = array [I] % 13; // determine the number switch (number) {Case 11: printf ("J"); break; case 12: printf ("Q"); break; Case 0: printf ("K"); break; default: printf ("% 2D", number); break ;} if (I % 4) = 0) // wrap {printf ("\ n") ;}} printf ("\ n");} int main () {int number [Max + 1] = {0}; // create a playing card initnumber (number, Max + 1); // initialize the playing card because it is not 0, therefore, the array length is + 1 randomsort (number, 1, max); // the playing cards are sorted printresult (number, Max + 1); return 1 ;}

 

 

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.