PAT 1042 Shuffling Machine

Source: Internet
Author: User
Tags shuffle

#include <cstdio>#include<cstdlib>#include<vector>using namespacestd;Chartbl[5] = {'S','H','C','D','J'};voidShuffle (vector<Char> &card, vector<Char> &Rnd) {    intrlen=rnd.size (); Vector<Char> tmp (card.size (),0);  for(intI=0; i<rlen; i++) {Tmp[rnd[i]]=Card[i]; } Card=tmp;}voidPrint_card (Charcard) {printf ("%c%d", tbl[card/ -], card% -+1);} intMain () {intTimes =0, r =0; Vector<Char> Rnd ( Wu,0); Vector<Char> Card ( Wu,0);  for(intI=0; i< Wu; i++) {Card[i]=i; } scanf ("%d", &Times );  for(intI=0; i< Wu; i++) {scanf ("%d", &R); Rnd[i]= R-1; }     for(intI=0; i<times; i++) {Shuffle (card, RND); } print_card (card[0]);  for(intI=1; i< Wu; i++) {printf (" ");    Print_card (Card[i]); }    return 0;}

At first it was complicated to think that the element exchange, as mentioned in the introduction to the algorithm, is due to the fact that the random sequence is not guaranteed to be unique within a certain range (if it is actually possible to get such a sequence, is equivalent to having already had a shuffle) so the number of random numbers that are generated when iterating through an array is to swap the current and subscript elements with the same element as the current random number. However, it has been said that the sequence is not repeated directly to the elements placed in the corresponding position. Of course, the program can not be a single shuffle written as a function, so as to avoid the creation of vectors repeatedly, as long as a temporary vector, a few more swap to replace the element copy.

PAT 1042 Shuffling Machine

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.