题目描述

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了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>using namespace std;int main()
{string a="0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX00000";//题目中n小于1000,上一行是为了获得1000个字符;int n;cin>>n;string t=a.substr(1,n);int s=t.length();string s1,s2;string s3=t;string input;int output=0;cin>>input;if(input=="out"){if(s%2==0){while(1){s1=s3.substr(0,s/2);s2=s3.substr(s/2,s/2);//cout<<s1<<s2;int i1=0;for(int i=0;i<s/2;i++){s3[i1++]=s1[i];s3[i1++]=s2[i];}output++;//cout<<output;//cout<<s3;if(s3==t){printf("%d",output);output=0;break;}}}else{while(1){s1=s3.substr(0,s/2+1);s2=s3.substr(s/2+1,s/2);//cout<<s1<<s2;int i1=0;for(int i=0;i<s/2;i++){s3[i1++]=s1[i];s3[i1++]=s2[i];}s3[i1]=s1[s/2];output++;//cout<<output;//cout<<s3;if(s3==t){printf("%d",output);output=0;break;}}}}if(input=="in"){if(s%2==0){while(1){s1=s3.substr(0,s/2);s2=s3.substr(s/2,s/2);//cout<<s1<<s2;int i1=0;for(int i=0;i<s/2;i++){s3[i1++]=s2[i];s3[i1++]=s1[i];}output++;//cout<<output;//cout<<s3;if(s3==t){printf("%d",output);output=0;break;}}}else{while(1){s1=s3.substr(0,s/2);s2=s3.substr(s/2,s/2+1);//cout<<s1<<s2;int i1=0;for(int i=0;i<s/2;i++){s3[i1++]=s2[i];s3[i1++]=s1[i];}s3[i1]=s2[s/2];output++;//cout<<output;//cout<<s3;if(s3==t){printf("%d",output);output=0;break;}}}}return 0;
}

2106 Problem F Shuffling Along相关推荐

  1. 2018 Multi-University Training Contest 3 Problem F. Grab The Tree 【YY+BFS】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6324 Problem F. Grab The Tree Time Limit: 2000/1000 MS ...

  2. Problem F: 结构体--学生信息排序

    Problem F: 结构体–学生信息排序 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 378 Solved: 192 [Submit][Status ...

  3. Problem F: Matrix Problem (III) : Array Practice Time Limit: 1 Sec Memory Limit: 4 MB Submit: 8787

    Problem F: Matrix Problem (III) : Array Practice Time Limit: 1 Sec  Memory Limit: 4 MB Submit: 8787  ...

  4. Problem F: 凹凸四边形

    Problem F: 凹凸四边形 Description 四边形分为凸四边形和凹四边形,如下图,图1为凸四边形,图2为凹四边形. 按照连边顺序给出四边形的四个顶点坐标,判断该四边形是凹四边形还是凸四边 ...

  5. Problem F: 计票

    Problem F: 计票 Description 美国总统大选终于拉开了正式帷幕,大家都预计希拉里会获胜.各州的统计结果陆续出来了,你能帮忙统计下总得票吗? Input 多组测试数据,每组先输入一个 ...

  6. Problem F: 一天中的第几秒

    实验4 Problem F: 一天中的第几秒 Description 一天24小时,每小时60分钟,每分钟60秒.一天共有86400秒. 0点0分0秒是每天的第1秒: 0点0分1秒是每天的第2秒: 0 ...

  7. Problem F. 洗衣服

    Problem F. 洗衣服 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) T ...

  8. 算法实验三 Problem F木乃伊迷宫

    Problem F 木乃伊迷宫 时限:1000ms 内存限制:10000K 总时限:3000ms 描述: 木乃伊地下宫殿是一个6行6列的迷宫.作为敢到木乃伊地下宫殿里去探险的你,有没有跟木乃伊抓迷藏的 ...

  9. Problem F: 乌鸦坐飞机

    Problem F: 乌鸦坐飞机 Time Limit: 2 Sec   Memory Limit: 128 MB Submit: 110   Solved: 9 [ Submit][ Status] ...

最新文章

  1. C++语言基本类型—字符型
  2. 实验三linux服务与进程管理,Linux 进程与服务管理1
  3. 解析不是utf-8的xml文件 附(tag 属性的获取 )
  4. 通信专业学python有用吗-通信算法工程师需要学python吗
  5. 对 UI 设计师来说,iPhone X 意味着什么?
  6. DAC MAC RBAC ABAC 权限系统的设计
  7. 洛谷 P1309 瑞士轮 解题报告
  8. 我们怎样确保从大数据计算中获得价值
  9. 如何写一手好 SQL!!!
  10. ADO SQL手写分页
  11. 小学生c语言入门教程,啊哈C语言(小学生坐在马桶上都能看懂C语言入门教程).pdf...
  12. “互联网+”医疗服务
  13. [NISACTF 2022]
  14. ubuntu安装独显驱动(R7000P RTX2060)
  15. 叮咚智能音箱使用体验:好看的皮囊千篇一律,有趣的灵魂万里挑一
  16. R语言数据可视化-条形图
  17. 《计算机达人成长之路——憧憬与迷茫篇》有钱的捧个预订场,有人的捧个评价场...
  18. (跟我一起来学区块链(1.9))之 区块链的应用前景
  19. python读取excel的路径
  20. 《MySQL性能优化和高可用架构实践》阅读总结

热门文章

  1. 快手还是慢手——问题解决及构想力
  2. 【Android -- RxJava】RxJava2.0 教程(七),如何使用 Flowable
  3. 到底什么是BFC?一篇就够了
  4. Mybatis-plus的update方法
  5. Unity 移动端禁止多点触摸,禁止多点操作
  6. 类打地鼠canvas小游戏
  7. python没有那个文件或目录_Python中的“没有这样的文件或目录”
  8. 2023成都信息工程大学计算机考研信息汇总
  9. CSDN markdown编辑器设置文字图片对齐方式
  10. web课程设计 简单的学生网页作业源码 基于html css javascript jquery女性化妆品商城