• 2106 Problem F Shuffling Along 中石油-未提交-->已提交


    题目描述

    Most of you have played card games (and if you haven’t, why not???) in which the deck of cards is randomized by shuffling it one or more times.
    A perfect shuffle is a type of shuffle where the initial deck is divided exactly in half, and the two halves are perfectly interleaved. For example, a deck consisting of eight cards ABCDEFGH (where A is the top card of the deck) would be divided into two halves ABCD and EFGH and then
    interleaved to get AEBFCGDH. Note that in this shuffle the original top card (A) stays on top —this type of perfect shuffle is called an out-shuffle. An equally valid perfect shuffle would start with the first card from the second half and result in EAFBGCHD — this is known as an in-shuffle.
    While normal shuffling does a good job at randomizing a deck, perfect shuffles result in only a small number of possible orderings. For example, if we perform multiple out-shuffles on the deck above, we obtain the following:
    ABCDEFGH → AEBFCGDH → ACEGBDFH → ABCDEFGH → · · ·
    So after 3 out-shuffles, the deck is returned to its original state. A similar thing happens if we perform multiple in-shuffles on an 8-card deck, though in this case it would take 6 shuffles before we get back to where we started. With a standard 52 card deck, only 8 out-shuffles are needed before the deck is returned to its original order (talented magicians can make use of this result in many of their tricks). These shuffles can also be used on decks with an odd number of cards, but we have to be a little careful: for out-shuffles, the first half of the deck must have 1 more card than the
    second half; for in-shuffles, it’s the exact opposite. For example, an out-shuffle on the deck ABCDE results in ADBEC, while an in-shuffle results in CADBE.
    For this problem you will be given the size of a deck and must determine how many in- or out-shuffles it takes to return the deck to its pre-shuffled order.

    输入

    The input consists of one line containing a positive integer n ≤ 1000 (the size of the deck) followed by either the word in or out, indicating whether you should perform in-shuffles or out-shuffles.

    输出

    For each test case, output the case number followed by the number of in- or out-shuffles required to return the deck to its original order.

    样例输入

    8 out
    

    样例输出

    3

    解题心得:
      
    题意:有n张牌,让你进行洗牌,最完美的一种是交替插入,举例:有ABCDEFGH八张(偶数张牌),分成ABCD,EFGH两组,
    按照“出-洗牌”之后成为AEBFCGDH,按照”入-洗牌“之后是EAFBGCHD.奇数张牌的时候:ABCDE,按照“出-  洗牌”之后成为ADBEC,按照”入-洗牌“之后是CADBE.输出洗牌几次之后会变成初始的序列。
      做之前需要判断是出洗牌,还是入洗牌,然后再判断有奇数张牌还是偶数张牌。只要写出其中一种情况,其余的再做微调就OK了。
    代码:
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 
      5 using namespace std;
      6 
      7 int main()
      8 {
      9     string a="0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX00000";
     10     //题目中n小于1000,上一行是为了获得1000个字符;
     11     int n;
     12     cin>>n;
     13     string t=a.substr(1,n);
     14     int s=t.length();
     15     string s1,s2;
     16     string s3=t;
     17     string input;
     18     int output=0;
     19     cin>>input;
     20     if(input=="out"){
     21         if(s%2==0){
     22             while(1){
     23                 s1=s3.substr(0,s/2);
     24                 s2=s3.substr(s/2,s/2);
     25                 //cout<<s1<<s2;
     26                 int i1=0;
     27                 for(int i=0;i<s/2;i++){
     28                     s3[i1++]=s1[i];
     29                     s3[i1++]=s2[i];
     30                 }
     31                 output++;
     32                 //cout<<output;
     33                 //cout<<s3;
     34                 if(s3==t){
     35                     printf("%d",output);
     36                     output=0;
     37                     break;
     38                 }
     39             }
     40         }else{
     41             while(1){
     42                 s1=s3.substr(0,s/2+1);
     43                 s2=s3.substr(s/2+1,s/2);
     44                 //cout<<s1<<s2;
     45                 int i1=0;
     46                 for(int i=0;i<s/2;i++){
     47                     s3[i1++]=s1[i];
     48                     s3[i1++]=s2[i];
     49                 }
     50                 s3[i1]=s1[s/2];
     51                 output++;
     52                 //cout<<output;
     53                 //cout<<s3;
     54                 if(s3==t){
     55                     printf("%d",output);
     56                     output=0;
     57                     break;
     58                 }
     59             }
     60         }
     61     }
     62     if(input=="in"){
     63         if(s%2==0){
     64             while(1){
     65                 s1=s3.substr(0,s/2);
     66                 s2=s3.substr(s/2,s/2);
     67                 //cout<<s1<<s2;
     68                 int i1=0;
     69                 for(int i=0;i<s/2;i++){
     70                     s3[i1++]=s2[i];
     71                     s3[i1++]=s1[i];
     72                 }
     73                 output++;
     74                 //cout<<output;
     75                 //cout<<s3;
     76                 if(s3==t){
     77                     printf("%d",output);
     78                     output=0;
     79                     break;
     80                 }
     81             }
     82         }else{
     83             while(1){
     84                 s1=s3.substr(0,s/2);
     85                 s2=s3.substr(s/2,s/2+1);
     86                 //cout<<s1<<s2;
     87                 int i1=0;
     88                 for(int i=0;i<s/2;i++){
     89                     s3[i1++]=s2[i];
     90                     s3[i1++]=s1[i];
     91                 }
     92                 s3[i1]=s2[s/2];
     93                 output++;
     94                 //cout<<output;
     95                 //cout<<s3;
     96                 if(s3==t){
     97                     printf("%d",output);
     98                     output=0;
     99                     break;
    100                 }
    101             }
    102         }
    103     }
    104 
    105     return 0;
    106 }
     


  • 相关阅读:
    解析大型.NET ERP系统 20条数据库设计规范
    vi显示行号
    shell awk
    Linux使用Shell脚本实现ftp的自动上传下载
    MySQL Replication的Reset slave重置命令
    怎么样调整FreeBSD时区问题
    Basic Memory Structures
    States of Integrity Constraints
    Merging into a Table: Example
    oracle 单独开始一个事物的写法 。
  • 原文地址:https://www.cnblogs.com/TWS-YIFEI/p/5574876.html
Copyright © 2020-2023  润新知