PAT-甲級-1042-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.

輸入格式:

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.

輸出格式:

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.

輸入樣例:

2
36 52 37 38 3 39 40 53 54 41 11 12 13 42 43 44 2 4 23 24 25 26 27 6 7 8 48 49 50 51 9 10 14 15 16 5 17 18 19 1 20 21 22 28 29 30 31 32 33 34 35 45 46 47

輸出樣例:

S7 C11 C10 C12 S1 H7 H8 H9 D8 D9 S11 S12 S13 D10 D11 D12 S3 S4 S6 S10 H1 H2 C13 D2 D3 D4 H6 H3 D13 J1 J2 C1 C2 C3 C4 D1 S5 H5 H11 H12 C6 C7 C8 C9 S2 S8 S9 H10 D5 D6 D7 H4 H13 C5

題目信息:

作者: CHEN, Yue

單位: 浙江大學

時間限制: 400 ms

內存限制: 64 MB

代碼長度限制: 16 KB

題目大意

自動洗牌機

這臺機器按照給定的隨機順序洗牌一副54張牌,並重復一定次數。假設卡片組的初始狀態如下:

S1,S2,…,S13,

H1,H2,…,H13,

C1,C2,…,C13,

D1,D2,…,D13,

J1、J2

其中“S”代表“黑桃♠”,“H”代表“紅桃♥”,“C”代表“梅花♣”,“D”代表“方片♦”,“J”代表“小丑(王)”。給定的順序是[1,54]中不同整數的排列。如果第i個位置的數字是j,則意味着將卡從位置i移動到位置j。例如,假設我們只有5張卡:S3、H5、C1、D13和J2。給定洗牌順序{4,2,5,3,1},結果將是:J2,H5,D13,S3,C1。如果我們再次重複洗牌,結果將是:C1、H5、S3、J2、D13。

每個輸入文件包含一個測試用例。

第一行包含一個正整數K(≤20),它是重複次數。

下一行包含給定的順序。一行中的所有數字都用空格隔開。

輸出規格:
對於每個測試用例,在一行中打印洗牌結果。所有的卡片都被一個空格隔開,行的末尾不能有多餘的空格。

分析

題目中先給定一個初始的牌序

第一行輸入洗牌的次數

第二行輸入洗牌的方式

舉個例子,假設 初始順序爲 H1, C2, J2, D13

洗牌方式爲change[2, 3, 4, 1],洗一次就是說把第一張牌放到位置1、第二張牌放到位置2、第三張牌放到位置3、第四張牌放到位置0,結果爲:D13, H1, C2, J2

洗第二次就是在第一次洗完的結果上再重複上述操作

實際上,第一次洗牌之後,第一張牌會到位置change[0]、也就是位置2上

第二次洗牌後,第一張牌就到了位置change[change[0]],即位置change[2] ,也就是位置3

所以我們可以不用真的洗牌,只要這樣一層一層嵌套,讀取到每張牌最終位置就可以,之後把這張牌放到最終位置上

最後順序輸出

代碼步驟

  1. 對於每張牌來說
    1. 首先設置本張牌第一次洗牌之後的位置pos

      • 注意可能洗0次
    2. 獲取本張牌 在第一次洗牌的基礎上 總次數-1 次洗牌之後的位置

      for(int j=1;j<time;j++)
          pos=change[pos];
      

代碼實現-JAVA

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception{
		Scanner sc = new Scanner(System.in);
        
        // 洗牌的次數
		int time = sc.nextInt();

		// 牌的初始順序
		String[] flower = new String[54];
		// 洗牌方式
		int[] change = new int[54];
		// 一次變化之後的位置
		int pos;
		// 牌的最終順序
		int[] end = new int[54];
		
        // 讀入洗牌方式
		for(int i = 0; i<54; i++) change[i]=sc.nextInt()-1;
        
        // 設置牌的初始順序
		int base=-1;
		for(int i=1; i<=13; i++) flower[i+base] = "S"+i; base+=13;
		for(int i=1; i<=13; i++) flower[i+base] = "H"+i; base+=13;
		for(int i=1; i<=13; i++) flower[i+base] = "C"+i; base+=13;
		for(int i=1; i<=13; i++) flower[i+base] = "D"+i; base+=13;
		flower[52] = "J1"; flower[53]="J2";
		
		// 對每張牌(共54張)都獲取最終位置
		for(int i = 0; i < 54; i++) {
            // 輸入排序次數可能是0
			if(time>0)
                pos = change[i]; // 獲取一次的位置
			else pos=i;
            // 獲取 總次數-1 次之後的位置
			for(int j=1;j<time;j++)
                pos=change[pos];
            // 將本張牌放在最終位置上
			end[pos]=i;
		}
        // 順序輸出
		for(int i=0;i<53;i++) {
			System.out.print(flower[end[i]]+" ");
		}
        System.out.println(flower[end[53]]);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章