UV problem solution: 10205-stack 'em up

Source: Internet
Author: User
Tags shuffle

Simple simulation will do the job.

Code:

  1. /*************************************** **********************************
  2. * Copyright (c) 2008 by liukaipeng *
  3. * Liukaipeng at gmail dot com *
  4. **************************************** *********************************/
  5. /* @ Judge_id 00000 10205 C "Stack 'em up "*/
  6. # Include <stdio. h>
  7. # Include <stdlib. h>
  8. # Include <string. h>
  9. # Include <strings. h>
  10. # Define nshuffles 100
  11. # Define nsuits 4
  12. # Define nvalues 13
  13. # Define ncards (nsuits * nvalues)
  14. Typedef int shuffle_t [ncards + 2];
  15. Typedef int deck_t [ncards + 2];
  16. Void read_shuffles (shuffle_t shuffles [], int nshuffles)
  17. {
  18. Int I, J;
  19. For (I = 1; I <= nshuffles; ++ I ){
  20. For (j = 1; j <= ncards; ++ J ){
  21. Scanf ("% d", & shuffles [I] [J]);
  22. }
  23. }
  24. Getchar ();
  25. }
  26. Void apply_shuffle (deck_t deck, shuffle_t shuffle)
  27. {
  28. Int M [ncards + 2] = {0 };
  29. Int H = 1, t = 1, s;/* head, target and source */
  30. Int c = deck [H];/* the card on the head */
  31. While (H <= ncards ){
  32. S = shuffle [T];
  33. M [s] = 1;
  34. If (s! = H ){
  35. Deck [T] = deck [s];
  36. T = s;
  37. } Else {
  38. Deck [T] = C;
  39. While (M [++ H])/* NOP */;
  40. T = h;
  41. C = deck [H];
  42. }
  43. }
  44. }
  45. Void init_deck (deck_t deck)
  46. {
  47. Int I;
  48. For (I = 1; I <= ncards; ++ I ){
  49. Deck [I] = I-1;
  50. }
  51. }
  52. Void print_deck (deck_t deck)
  53. {
  54. Char * svalues [] = {
  55. "2", "3", "4", "5", "6", "7", "8", "9", "10 ",
  56. "Jack", "Queen", "King", "Ace ",
  57. };
  58. Char * ssuits [] = {
  59. "Clubs", "diamonds", "Hearts", "Spades ",
  60. };
  61. Int I;
  62. For (I = 1; I <= ncards; ++ I ){
  63. Char Buf [20];
  64. Strcpy (BUF, svalues [deck [I] % nvalues]);
  65. Strcat (BUF, "");
  66. Strcat (BUF, ssuits [deck [I]/nvalues]);
  67. Puts (BUF );
  68. }
  69. }
  70. Int main (INT argc, char * argv [])
  71. {
  72. # Ifndef online_judge
  73. Char in [256];
  74. Char out [256];
  75. Strcpy (in, argv [0]);
  76. Strcat (in, ". In ");
  77. Strcpy (Out, argv [0]);
  78. Strcat (Out, ". Out ");
  79. If (freopen (in, "r", stdin) = NULL)
  80. Fprintf (stderr, "% s: cannot open input file: % S./N", argv [0], in );
  81. If (freopen (Out, "W", stdout) = NULL)
  82. Fprintf (stderr, "% s: cannot open output file: % S./N", argv [0], out );
  83. # Endif
  84. Int ncases;
  85. Scanf ("% d/N", & ncases );
  86. While (ncases --> 0 ){
  87. Int nshuffles;
  88. Scanf ("% d/N", & nshuffles );
  89. Shuffle_t shuffles [nshuffles + 1];
  90. Read_shuffles (shuffles, nshuffles );
  91. Deck_t deck;
  92. Init_deck (deck );
  93. Char Buf [10];
  94. While (fgets (BUF, 10, stdin )! = NULL & Buf [0]! = '/N '){
  95. Apply_shuffle (deck, shuffles [atoi (BUF)]);
  96. }
  97. Print_deck (deck );
  98. If (ncases> 0 ){
  99. Putchar ('/N ');
  100. }
  101. }
  102. Return 0;
  103. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.