00-自测5. Shuffling Machine (20)
Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid "inside jobs" where employees collaborate with gamblers by performing inadequate shuffles, many casinos employ automatic shuffling machines. Your task is to simulate a shuffling machine.
The machine shuffles a deck of 54 cards according to a given random order and repeats for a given number of times. It is assumed that the initial status of a card deck is in the following order:
S1, S2, ..., S13, H1, H2, ..., H13, C1, C2, ..., C13, D1, D2, ..., D13, J1, J2
where "S" stands for "Spade", "H" for "Heart", "C" for "Club", "D" for "Diamond", and "J" for "Joker". A given order is a permutation of distinct integers in [1, 54]. If the number at the i-th position is j, it means to move the card from position i to position j. For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer K (<= 20) which is the number of repeat times. Then the next line contains the given order. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the shuffling results in one line. All the cards are separated by a space, and there must be no extra space at the end of the line.
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 5 public class Main 6 { 7 static int n; 8 static String[] card=new String[54]; 9 static int[] order=new int[54]; 10 public static void main(String[] args) throws IOException 11 { 12 cardset(); 13 BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); 14 String line; 15 line=buf.readLine(); 16 n=Integer.parseInt(line); 17 line=buf.readLine(); 18 String[]temp=line.split(" "); 19 orderset(temp); 20 for(int i=0;i<n;i++) 21 play(); 22 for(int i=0;i<53;i++) 23 System.out.print(card[i]+" "); 24 System.out.println(card[53]); 25 26 27 } 28 static private void cardset() 29 { 30 for(int i=0;i<4;i++) 31 { 32 String[] t={"S","H","C","D"}; 33 for(int j=0;j<13;j++) 34 { 35 card[i*13+j]=t[i]+(j+1); 36 } 37 } 38 card[52]="J1"; 39 card[53]="J2"; 40 // for(int i=0;i<54;i++) 41 // System.out.println(card[i]); 42 } 43 static private void orderset(String[]temp) 44 { 45 for(int i=0;i<54;i++) 46 { 47 order[i]=Integer.parseInt(temp[i]); 48 } 49 // for(int i=0;i<54;i++) 50 // System.out.println(order[i]); 51 52 } 53 static private void play() 54 { 55 String t1,t2; 56 boolean[] flag=new boolean[54]; 57 for(int i=0;i<54;i++) 58 flag[i]=false; 59 for(int i=0;i<54;i++) 60 { 61 if(flag[i]) 62 continue; 63 t1=card[i]; 64 for(int j=order[i]-1;!flag[i];j=order[j]-1) 65 { 66 t2=card[j]; 67 card[j]=t1; 68 t1=t2; 69 flag[j]=true; 70 } 71 } 72 // for(int i=0;i<54;i++) 73 // System.out.print(card[i]+" "); 74 // System.out.println(); 75 } 76 } 77