zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    2018年秋季个人阅读计划
    java当中JDBC当中JNDI用来查找dataSource的例子
    为什么要引入激活函数?
    为什么引入神经网络来做识别,判断,预测?
    给出一个生活中的最简单的两层神经网的实际例子
    MapReduce的输入文件是两个
    hadoop在eclipse当中如何添加源码?
    MapReduce的shuffle过程详解
    hadoop WordCount例子详解。
    Hadoop的eclipse的插件是怎么安装的?
  • 原文地址:https://www.cnblogs.com/qq1029579233/p/4399101.html
Copyright © 2011-2022 走看看