题目描述

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(,n);
int s=t.length();
string s1,s2;
string s3=t;
string input;
int output=;
cin>>input;
if(input=="out"){
if(s%==){
while(){
s1=s3.substr(,s/);
s2=s3.substr(s/,s/);
//cout<<s1<<s2;
int i1=;
for(int i=;i<s/;i++){
s3[i1++]=s1[i];
s3[i1++]=s2[i];
}
output++;
//cout<<output;
//cout<<s3;
if(s3==t){
printf("%d",output);
output=;
break;
}
}
}else{
while(){
s1=s3.substr(,s/+);
s2=s3.substr(s/+,s/);
//cout<<s1<<s2;
int i1=;
for(int i=;i<s/;i++){
s3[i1++]=s1[i];
s3[i1++]=s2[i];
}
s3[i1]=s1[s/];
output++;
//cout<<output;
//cout<<s3;
if(s3==t){
printf("%d",output);
output=;
break;
}
}
}
}
if(input=="in"){
if(s%==){
while(){
s1=s3.substr(,s/);
s2=s3.substr(s/,s/);
//cout<<s1<<s2;
int i1=;
for(int i=;i<s/;i++){
s3[i1++]=s2[i];
s3[i1++]=s1[i];
}
output++;
//cout<<output;
//cout<<s3;
if(s3==t){
printf("%d",output);
output=;
break;
}
}
}else{
while(){
s1=s3.substr(,s/);
s2=s3.substr(s/,s/+);
//cout<<s1<<s2;
int i1=;
for(int i=;i<s/;i++){
s3[i1++]=s2[i];
s3[i1++]=s1[i];
}
s3[i1]=s2[s/];
output++;
//cout<<output;
//cout<<s3;
if(s3==t){
printf("%d",output);
output=;
break;
}
}
}
} return ;
}
 
												

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. 论Linux运维的一些基础安全知识和简单办法

    不知不觉本人来北京也已经第三个年头了,从一个Linux小小鸟,开始,2012年我参加了第一份工作,其实现在想想其实我是幸运的,本来求学的时候,就没好好的学Linux,我认为有Cisco知识从上wind ...

  2. pyspider安装

    官方文档上说的比较简单: pip install pyspider 但是实际安装时还是有些问题导致无法成功. windows下安装 先安装PhantomJS 可以依照自己的开发平台选择不同的包进行下载 ...

  3. C语言:break和continue

    break: break用于循环则是终止循环,break如果用于switch,则是用于终止switch.break不能直接用于if,除非if是属于循环内部的一个子句 ;i<;i++) { > ...

  4. 【bzoj4027】[HEOI2015]兔子与樱花

    题目描述 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1个树枝连接,我们可以把它 ...

  5. 第26天 swift reflect

    1.reflect http://www.jianshu.com/p/b5c87824e33c 2.android studio 2.0项目1.5编译 Cause: com/android/build ...

  6. MFC线程内操作主窗体 控件

    CWnd* h_d2 = AfxGetApp()->GetMainWnd(); //获取主窗口的句柄 h_d2-> GetDlgItem(IDC_EDIT2)->GetWindowT ...

  7. [开源类库/项目] android保存崩溃时的错误信息log至本地【源码+jar包+使用说...

    不知大家是否经常遇到这种情况:自己的项目有时会在没有连接到电脑时发生崩溃,好不容易发现的bug结果连接到电脑时又复现不出来了:又或者自己写的一个功能在开机启动时产生小bug导致崩溃,而刚启动的机器想让 ...

  8. [Asp.net MVC]Asp.net MVC5系列——第一个项目

    目录 概述 创建第一个项目 添加控制器 总结 概述 本教程是个人一步一步学习的总结,希望能帮到正在进入ASP.Net MVC5方向的朋友,个人也是准备进入ASP.NET MVC5领域,虽然艰辛,但是乐 ...

  9. Two Strings Are Anagrams

    Write a method anagram(s,t) to decide if two strings are anagrams or not. 判断两个字符串里的字符是否相同,也就是是否能够通过改 ...

  10. 一起入门python6之函数

    今天我们来学习新的一篇吧,那便是“函数(function)”我们用def来定义一个函数,以案例说话.>>> def name(x):          #定义一个“name”的函数. ...