#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