题目描述

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. SQL语句学习笔记

    从外部EXCEl文件导入sqlserver数据库操作命令 reconfigure reconfigure go select * into abc1_1 from OPENROWSET('MICROS ...

  2. HTML的内联元素换行问题

    一般a.span.label多个组合,需要换行时,使用以下CSS来处理: white-space: nowrap; display: inline-block;

  3. Oracle卸载

    用Oracle自带的卸载程序不能从根本上卸载Oracle,从而为下次的安装留下隐患,所以要想完全卸载Oracle就必须要直接将注册表清除. 步骤如下: 1. 开始->设置->控制面板-&g ...

  4. session 的用法

    </head> <body> <?php //session_start();//开启session,必须写在PHP代码最顶端 //HTTP,无状态性 //记录登陆者状态 ...

  5. redis--key1

    package com.ztest.redis; import java.util.Set; import com.sun.istack.internal.logging.Logger; import ...

  6. JQuery在asp.net中三种ajax传值

    1)通过webservice,注意去掉注释[System.Web.Script.Services.ScriptService]这行前的注释 2)通过aspx.cs文件中的静态方法 3)通过aspx文件 ...

  7. 汉字转拼音(pinyin4j)

    1.引入依赖 <dependency> <groupId>pinyin4j.sourceforge.net</groupId> <artifactId> ...

  8. CentOS 6.5 zabbix 3.0.4 SendEmail报警

    官方介绍:http://caspian.dotconf.net/menu/Software/SendEmail/ 1.sendEmail部署 下载安装包到本地.解压 [root@localhost S ...

  9. Memcached和Redis对比和适用场景

    关于memcached和redis的使用场景,根据大神们的讨论和我在网上查到的资料,总结一下: 两者对比: redis提供数据持久化功能,memcached无持久化: redis的数据结构比memca ...

  10. hibernate 的三种状态 如何转化的。

    1. 临时状态  由 new命令开辟内存空间的java对象,例如: User user=new User(); 临 时对象在内存孤立存在,它是携带信息的载体,不和数据库的数据有任何关联关系.   2. ...