zoukankan      html  css  js  c++  java
  • 8.1.4 Flipper

    Flipper

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 39 Accepted Submission(s): 29

    Problem Description
    Little Bobby Roberts (son of Big Bob, of Problem G) plays this solitaire memory game called Flipper. He starts with n cards, numbered 1 through n, and lays them out in a row with the cards in order left-to-right. (Card 1 is on the far left; card n is on the far right.) Some cards are face up and some are face down. Bobby then performs n - 1 flips — either right flips or left flips. In a right flip he takes the pile to the far right and flips it over onto the card to its immediate left. For example, if the rightmost pile has cards A, B, C (from top to bottom) and card D is to the immediate left, then flipping the pile over onto card D would result in a pile of 4 cards: C, B, A, D (from top to bottom). A left flip is analogous.

    The very last flip performed will result in one pile of cards — some face up, some face down. For example, suppose Bobby deals out 5 cards (numbered 1 through 5) with cards 1 through 3 initially face up and cards 4 and 5 initially face down. If Bobby performs 2 right flips, then 2 left flips, the pile will be (from top to bottom) a face down 2, a face up 1, a face up 4, a face down 5, and a face up 3.

    Now Bobby is very sharp and you can ask him what card is in any position and he can tell you!!! You will write a program that matches Bobby’s amazing feat.
     

    Input
    Each test case will consist of 4 lines. The first line will be a positive integer n (2 ≤ n ≤ 100) which is the number of cards laid out. The second line will be a string of n characters. A character U indicates the corresponding card is dealt face up and a character D indicates the card is face down. The third line is a string of n - 1 characters indicating the order of the flips Bobby performs. Each character is either R, indicating a right flip, or L, indicating a left flip. The fourth line is of the form m q1 q2 . . . qm, where m is a positive integer and 1 ≤ qin. Each qi is a query on a position of a card in the pile (1 being the top card, n being the bottom card). A line containing 0 indicates end of input.
     

    Output
    Each test case should generate m + 1 lines of output. The first line is of the form
    Pile t
    where t is the number of the test case (starting at 1). Each of the next m lines should be of the form
    Card qi is a face up k.
    or
    Card qi is a face down k.
    accordingly, for i = 1, ..,m, where k is the number of the card.
    For instance, in the above example with 5 cards, if qi = 3, then the answer would be
    Card 3 is a face up 4.
     

    Sample Input
    5
    UUUDD
    RRLL
    5 1 2 3 4 5
    10
    UUDDUUDDUU
    LLLRRRLRL
    4 3 7 6 1
    0
     

    Sample Output
    Pile 1
    Card 1 is a face down 2.
    Card 2 is a face up 1.
    Card 3 is a face up 4.
    Card 4 is a face down 5.
    Card 5 is a face up 3.
    Pile 2
    Card 3 is a face down 1.
    Card 7 is a face down 9.
    Card 6 is a face up 7.
    Card 1 is a face down 5.

    很好的一道题,当时怎么就没做了。。

     1 #include<stdio.h>
     2 #include<string.h>
     3 int map[101][101];
     4 int work[101];
     5 
     6 int main() {
     7     int n, i, j, m, l, r, v = 1, x, k;
     8     char str[101];
     9     char c;
    10     int num[101];
    11     while (scanf("%d", &n), n) {
    12         memset(work, 0, sizeof (work));
    13         memset(num, 0, sizeof (num));
    14         scanf("%s", str);
    15        
    16         for (i = 1; i <= n; i++) {
    17             map[i][1] = i;
    18             num[i] = 1;
    19         }
    20         l = 1, r = n;
    21         getchar();
    22         for (i = 1; i < n; i++) {
    23             scanf("%c", &c);
    24             if (c == 'R') {
    25                 for (j = num[r]; j >= 1; j--) {
    26                     ++num[r - 1];
    27                     map[r - 1][num[r - 1]] = map[r][j];
    28                     work[map[r][j]]++;
    29                 }
    30                
    31                 r--;
    32             } else {
    33                 for (j = num[l]; j >= 1; j--) {
    34                     map[l + 1][++num[l + 1]] = map[l][j];
    35                     work[map[l][j]]++;
    36                 }
    37                 l++;
    38             }
    39 
    40         }
    41         printf("Pile %d
    ", v++);
    42         scanf("%d", &m);
    43         while (m-- && scanf("%d", &x)) {
    44             printf("Card %d is a face", x);
    45             k = map[l][n - x + 1];
    46             if (work[k] % 2 == 0) {
    47                 if (str[k - 1] == 'U')printf(" up ");
    48                 else printf(" down ");
    49             } else {
    50                 if (str[k - 1] == 'U')printf(" down ");
    51                 else printf(" up ");
    52             }
    53             printf("%d.
    ", k);
    54         }
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    Delphi异常处理try except语句和try finally语句用法以及区别
    test
    Infopath resource
    C# IDE
    操作数据库
    不同版本数据库的导入
    workflow for sharepoint 2007
    http://www.cnblogs.com/BearStudyHard/archive/2008/03/26/1123267.html
    深入浅出InfoPath——安装VSTO
    如何使用Lotuscript管理Excel中的工作表?
  • 原文地址:https://www.cnblogs.com/cssystem/p/3212481.html
Copyright © 2011-2022 走看看