zoukankan      html  css  js  c++  java
  • HDU3328_Flipper_纸牌翻转_模拟题

    /*
    *State 0MS    364K    1978 B    C++
    *题目大意:
    *        给定n堆牌,一开始每一堆牌有n张,然后有两种操作,一种是L翻转,一种
    *        是R翻转,L翻转是将最左边的那一堆全部翻转贴到最接近的右边那一堆的
    *        上方,R翻转类似,然后要求最后只剩下一堆的时候,各个牌的状态。
    *解题思路:
    *        直接用栈来模拟牌的翻转即可,一开始设置n个栈,然后用两个变量来指向
    *        最左边那一堆跟最右边那一堆,直到最后两个变量均指向同一堆,操作结束。
    *题目考点:
    *        比较基础,但稍微有一点点猥琐的模拟题。
    */
    View Code
      1 #include <iostream>
      2 #include <stack>
      3 using namespace std;
      4 
      5 const int MAX = 105;
      6 
      7 typedef struct node_
      8 {
      9     int updown;//up为1,down为0
     10     int id;
     11 }N;
     12 
     13 void dealOpe(int n, stack<N> S[], char ope[], N ans[], int &cnt)
     14 {
     15     int l = 0, r = n - 1;
     16     for(int i = 0; i < n - 1; i++)
     17     {
     18         if(ope[i] == 'L')
     19         {
     20             while(!S[l].empty())
     21             {
     22                 N t;
     23                 t.id = S[l].top().id;
     24                 t.updown = S[l].top().updown ^ 1;
     25                 S[l + 1].push(t);
     26                 S[l].pop();
     27             }
     28             l++;
     29         }
     30         else
     31         {
     32             while(!S[r].empty())
     33             {
     34                 N t;
     35                 t.id = S[r].top().id;
     36                 t.updown = S[r].top().updown ^ 1;
     37                 S[r - 1].push(t);
     38                 S[r].pop();
     39             }
     40             r--;
     41         }
     42     }
     43     while(!S[r].empty())
     44     {
     45         N tmp;
     46         tmp.id = S[r].top().id;
     47         tmp.updown = S[r].top().updown;
     48         ans[cnt++] = tmp;
     49         S[r].pop();
     50     }
     51     return ;
     52 }
     53 
     54 void output(int cnt, N ans[], int m, int arr[], int &cas_c)
     55 {
     56     printf("Pile %d\n", cas_c++);
     57     for(int i = 0; i < m; i++)
     58     {
     59         printf("Card %d is a", arr[i] + 1);
     60         if(ans[arr[i]].updown == 1)
     61             printf(" face up");
     62         else
     63             printf(" face down");
     64         printf(" %d.\n", ans[arr[i]].id);
     65     }
     66     return ;
     67 }
     68 
     69 int main(void)
     70 {
     71 #ifndef ONLINE_JUDGE
     72     freopen("in.txt", "r", stdin);
     73 #endif
     74     int n, cas_c = 1;
     75     while(scanf("%d", &n), n)
     76     {
     77         char updown[MAX], ope[MAX];
     78         scanf("%s %s", updown, ope);
     79         N temp;
     80         stack<N> S[MAX];
     81         for(int i = 0; i < n; i++)
     82         {
     83             temp.id = i + 1;
     84             if(updown[i] == 'U')
     85                 temp.updown = 1;
     86             else
     87                 temp.updown = 0;
     88             S[i].push(temp);
     89         }
     90         N ans[MAX];
     91         int cnt = 0;
     92         dealOpe(n, S, ope, ans, cnt);
     93 
     94         int m, arr[MAX];
     95         scanf("%d", &m);
     96         for(int i = 0; i < m; i++)
     97         {
     98             int t;
     99             scanf("%d", &t);
    100             t--, arr[i] = t;
    101         }
    102         output(cnt, ans, m, arr, cas_c);
    103     }
    104     return 0;
    105 }
  • 相关阅读:
    Spring Boot 2.4版本前后的分组配置变化及对多环境配置结构的影响
    Spring Boot 2.4 对多环境配置的支持更改
    Spring Boot 的2020最后一击:2.4.1、2.3.7、2.2.12 发布
    苹果M1芯片各种不支持,但居然可以刷朋友圈!你会买单吗?
    老板居然让我在Java项目中“造假”
    Spring Cloud正式移除Hystrix、Zuul等Netflix OSS组件
    为了Java微信支付V3开发包,我找出了微信支付文档至少六个错误
    IdentityServer4系列 | 支持数据持久化
    IdentityServer4系列 | 混合模式
    Gitlab Runner的分布式缓存实战
  • 原文地址:https://www.cnblogs.com/cchun/p/2607733.html
Copyright © 2011-2022 走看看