Simple simulation will do the job.
Code:
- /*************************************** **********************************
- * Copyright (c) 2008 by liukaipeng *
- * Liukaipeng at gmail dot com *
- **************************************** *********************************/
- /* @ Judge_id 00000 10205 C "Stack 'em up "*/
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <string. h>
- # Include <strings. h>
- # Define nshuffles 100
- # Define nsuits 4
- # Define nvalues 13
- # Define ncards (nsuits * nvalues)
- Typedef int shuffle_t [ncards + 2];
- Typedef int deck_t [ncards + 2];
- Void read_shuffles (shuffle_t shuffles [], int nshuffles)
- {
- Int I, J;
- For (I = 1; I <= nshuffles; ++ I ){
- For (j = 1; j <= ncards; ++ J ){
- Scanf ("% d", & shuffles [I] [J]);
- }
- }
- Getchar ();
- }
- Void apply_shuffle (deck_t deck, shuffle_t shuffle)
- {
- Int M [ncards + 2] = {0 };
- Int H = 1, t = 1, s;/* head, target and source */
- Int c = deck [H];/* the card on the head */
- While (H <= ncards ){
- S = shuffle [T];
- M [s] = 1;
- If (s! = H ){
- Deck [T] = deck [s];
- T = s;
- } Else {
- Deck [T] = C;
- While (M [++ H])/* NOP */;
- T = h;
- C = deck [H];
- }
- }
- }
- Void init_deck (deck_t deck)
- {
- Int I;
- For (I = 1; I <= ncards; ++ I ){
- Deck [I] = I-1;
- }
- }
- Void print_deck (deck_t deck)
- {
- Char * svalues [] = {
- "2", "3", "4", "5", "6", "7", "8", "9", "10 ",
- "Jack", "Queen", "King", "Ace ",
- };
- Char * ssuits [] = {
- "Clubs", "diamonds", "Hearts", "Spades ",
- };
- Int I;
- For (I = 1; I <= ncards; ++ I ){
- Char Buf [20];
- Strcpy (BUF, svalues [deck [I] % nvalues]);
- Strcat (BUF, "");
- Strcat (BUF, ssuits [deck [I]/nvalues]);
- Puts (BUF );
- }
- }
- Int main (INT argc, char * argv [])
- {
- # Ifndef online_judge
- Char in [256];
- Char out [256];
- Strcpy (in, argv [0]);
- Strcat (in, ". In ");
- Strcpy (Out, argv [0]);
- Strcat (Out, ". Out ");
- If (freopen (in, "r", stdin) = NULL)
- Fprintf (stderr, "% s: cannot open input file: % S./N", argv [0], in );
- If (freopen (Out, "W", stdout) = NULL)
- Fprintf (stderr, "% s: cannot open output file: % S./N", argv [0], out );
- # Endif
- Int ncases;
- Scanf ("% d/N", & ncases );
- While (ncases --> 0 ){
- Int nshuffles;
- Scanf ("% d/N", & nshuffles );
- Shuffle_t shuffles [nshuffles + 1];
- Read_shuffles (shuffles, nshuffles );
- Deck_t deck;
- Init_deck (deck );
- Char Buf [10];
- While (fgets (BUF, 10, stdin )! = NULL & Buf [0]! = '/N '){
- Apply_shuffle (deck, shuffles [atoi (BUF)]);
- }
- Print_deck (deck );
- If (ncases> 0 ){
- Putchar ('/N ');
- }
- }
- Return 0;
- }