题目描述

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.

样例输入

  1. 8 out

样例输出

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

2106 Problem F Shuffling Along 中石油-未提交-->已提交的更多相关文章

  1. 互联网项目中mysql推荐(读已提交RC)的事务隔离级别

    [原创]互联网项目中mysql应该选什么事务隔离级别 Mysql为什么不和Oracle一样使用RC,而用RR 使用RC的原因 这个是有历史原因的,当然要从我们的主从复制开始讲起了!主从复制,是基于什么 ...

  2. 2078 Problem H Secret Message 中石油-未提交-->已提交

    题目描述 Jack and Jill developed a special encryption method, so they can enjoy conversations without wo ...

  3. 翻译:如何向MariaDB中快速插入数据(已提交到MariaDB官方手册)

    本文为mariadb官方手册:How to Quickly Insert Data Into MariaDB的译文. 原文:https://mariadb.com/kb/en/how-to-quick ...

  4. Eclipse中使用GIT将已提交到本地的文件上传至远程仓库

    GIT将已提交到本地的文件上传至远程仓库: 1.  右击项目——Team——Push to Upstream,即可将已保存在本地的文件上传推至GIT远程仓库.

  5. 几何入门合集 gym101968 problem F. Mirror + gym102082 Problem F Fair Chocolate-Cutting + gym101915 problem B. Ali and Wi-Fi

    abstract: V const & a 加速 F. Mirror 题意 链接 问题: 有n个人在y=0的平面上(及xoz平面).z=0平面上有一面镜子(边平行于坐标轴).z=a平面上有q个 ...

  6. hpu第六次周赛Problem F

    Problem F Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  7. Problem F. Wiki with String

    Problem F. Wiki with StringInput file: standard input Time limit: 1 secondOutput file: standard outp ...

  8. 实验12:Problem F: 求平均年龄

    Home Web Board ProblemSet Standing Status Statistics   Problem F: 求平均年龄 Problem F: 求平均年龄 Time Limit: ...

  9. The Ninth Hunan Collegiate Programming Contest (2013) Problem F

    Problem F Funny Car Racing There is a funny car racing in a city with n junctions and m directed roa ...

随机推荐

  1. 了不起的Node.js读书笔记

    原文摘自我的前端博客,欢迎大家来访问 http://www.hacke2.cn 第二章 Js概览 基于GoogleV8引擎 Object.keys(o) 数组方法:遍历forEach.过滤filter ...

  2. C#集合类图继承关系一览表

  3. [译]Mongoose指南 - Document

    更新 有几种方式更新document. 先看一下传统的更新方法 Tank.findById(id, function(err, tank){ if(err) return handleError(er ...

  4. Tomcat 6.0 简介

    本片翻译来自:http://tomcat.apache.org/tomcat-6.0-doc/introduction.html 介绍 无论是开发者还是tomcat管理员在使用前都需要了解一些必要的信 ...

  5. SQL存储过程来调用webservice

    如果用存储过程来调用webservice 那存储过程的功能感觉能做好多事情了? 别自欺欺人了.那些功能还是webservice来实现的... 完整的webservice代码:(也是默认的,新建.asm ...

  6. 利用百度语音API进行语音识别。

    由于项目需要,这几天都在试图利用百度语音API进行语音识别.但是识别到的都是“啊,哦”什么的,我就哭了. 这里我只是分享一下这个过程,错误感觉出现在Post语音数据那一块,可能是转换问题吧. API请 ...

  7. asp.net—缓存

    1.页面缓存 要实现页面输出缓存,只要将一条 OutputCache 指令添加到页面即可. <%@ OutputCache CacheProfile=" " NoStore= ...

  8. tone mapping简介

    以下内容转自网络: tone Mapping原是摄影学中的一个术语,因为打印相片所能表现的亮度范围不足以表现现实世界中的亮度域,而如果简单的将真实世界的整个亮度域线性压缩到照片所能表现的亮度域内,则会 ...

  9. CSS3实现背景颜色渐变 摘抄

    一. Webkit浏览器 (1) 第一种写法: background:-webkit-gradient(linear ,10% 10%,100% 100%, color-stop(0.14,rgb(2 ...

  10. java项目导入IntelliJ IDEA

    (0)之所以有第0步,是因为第一次倒入失败,所以从删除上次倒入的数据开始- 开始删除数据. 启动Intelli J,点击右键删除上次的导入的项目 把配置拷贝到.m2文件夹下,并且删除上次下载的一些依赖 ...