程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu3328(翻轉card)

hdu3328(翻轉card)

編輯:C++入門知識

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 ≤ qi ≤ n. 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 twhere 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.
題意:其實這題並不難,只是題意有點難理解首先給出每張牌的初始狀態,U為面朝上,D為面朝下,然後跟著幾個操作,L代表從左邊翻,R代表從右邊翻例如第一個樣例,前兩個R,代表最右邊的5翻轉,疊放到4上,然後這兩張疊放的一起翻轉,疊放到3上剩下的兩個L,代表最左邊的1翻轉,疊峰到2上,然後這兩張再一起翻轉,疊放到3,4,5形成的堆上,然後他們就成為了一堆最後給出n詢問,詢問這一堆的第幾張牌是什麼狀態例如Card 1 is a face down 2.就是說這個堆的第一張牌是面朝下的,數字為2。

#include<stdio.h>   struct card  {      int data;      char dir;//面向上或下   };  struct card stack[105][105];  int had,end,top[105];    void Turn(char turn[],int n)  {      int i=0,tp;      while(i<n)//每一步       {          if(turn[i]=='R')//從右往左翻一個堆           {              tp=top[end];//當前要翻的棧裡面有多少張               while(tp>0)//按順序從上到下把全部的放入下一個棧裡               {                  if(stack[end][tp].dir=='U')                  stack[end][tp].dir='D';                  else                  stack[end][tp].dir='U';                    stack[end-1][++top[end-1]]=stack[end][tp];                  tp--;              }              top[end]=0; end--;//end是指向最右邊的一個棧           }          else          {              tp=top[had];              while(tp>0)              {                  if(stack[had][tp].dir=='U')                  stack[had][tp].dir='D';                  else                  stack[had][tp].dir='U';                    stack[had+1][++top[had+1]]=stack[had][tp];                  tp--;              }              top[had]=0; had++;//had是指向最左邊的一個棧           }          i++;//執行下一步       }  }    int main()  {      int n,i,m,a[105],t=1,k;      char turn[105];      while(scanf("%d",&n)>0&&n)      {          getchar();          for(i=1;i<=n;i++)//首先每個棧中只有一張card           {              scanf("%c",&stack[i][1].dir);              stack[i][1].data=i;              top[i]=1;          }          getchar();          scanf("%s",turn);//輸入要執行的步驟           scanf("%d",&m);          for(i=1;i<=m;i++)//輸入要在最後一堆中查找的位置           scanf("%d",&a[i]);          had=1;end=n;//初始指的位置             Turn(turn,n-1);//執行             printf("Pile %d\n",t++);          for(i=1;i<=m;i++)          {              k=top[end]-a[i]+1;              if(stack[end][k].dir=='U')              printf("Card %d is a face up %d.\n",a[i],stack[end][k].data);              else              printf("Card %d is a face down %d.\n",a[i],stack[end][k].data);          }      }  }  #include<stdio.h>
struct card
{
    int data;
    char dir;//面向上或下
};
struct card stack[105][105];
int had,end,top[105];

void Turn(char turn[],int n)
{
    int i=0,tp;
    while(i<n)//每一步
    {
        if(turn[i]=='R')//從右往左翻一個堆
        {
            tp=top[end];//當前要翻的棧裡面有多少張
            while(tp>0)//按順序從上到下把全部的放入下一個棧裡
            {
                if(stack[end][tp].dir=='U')
                stack[end][tp].dir='D';
                else
                stack[end][tp].dir='U';

                stack[end-1][++top[end-1]]=stack[end][tp];
                tp--;
            }
            top[end]=0; end--;//end是指向最右邊的一個棧
        }
        else
        {
            tp=top[had];
            while(tp>0)
            {
                if(stack[had][tp].dir=='U')
                stack[had][tp].dir='D';
                else
                stack[had][tp].dir='U';

                stack[had+1][++top[had+1]]=stack[had][tp];
                tp--;
            }
            top[had]=0; had++;//had是指向最左邊的一個棧
        }
        i++;//執行下一步
    }
}

int main()
{
    int n,i,m,a[105],t=1,k;
    char turn[105];
    while(scanf("%d",&n)>0&&n)
    {
        getchar();
        for(i=1;i<=n;i++)//首先每個棧中只有一張card
        {
            scanf("%c",&stack[i][1].dir);
            stack[i][1].data=i;
            top[i]=1;
        }
        getchar();
        scanf("%s",turn);//輸入要執行的步驟
        scanf("%d",&m);
        for(i=1;i<=m;i++)//輸入要在最後一堆中查找的位置
        scanf("%d",&a[i]);
        had=1;end=n;//初始指的位置

        Turn(turn,n-1);//執行

        printf("Pile %d\n",t++);
        for(i=1;i<=m;i++)
        {
            k=top[end]-a[i]+1;
            if(stack[end][k].dir=='U')
            printf("Card %d is a face up %d.\n",a[i],stack[end][k].data);
            else
            printf("Card %d is a face down %d.\n",a[i],stack[end][k].data);
        }
    }
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved