2106 Problem F Shuffling Along 中石油-未提交-->已提交
题目描述
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.
输入
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.
输出
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 中石油-未提交-->已提交的更多相关文章
- 互联网项目中mysql推荐(读已提交RC)的事务隔离级别
[原创]互联网项目中mysql应该选什么事务隔离级别 Mysql为什么不和Oracle一样使用RC,而用RR 使用RC的原因 这个是有历史原因的,当然要从我们的主从复制开始讲起了!主从复制,是基于什么 ...
- 2078 Problem H Secret Message 中石油-未提交-->已提交
题目描述 Jack and Jill developed a special encryption method, so they can enjoy conversations without wo ...
- 翻译:如何向MariaDB中快速插入数据(已提交到MariaDB官方手册)
本文为mariadb官方手册:How to Quickly Insert Data Into MariaDB的译文. 原文:https://mariadb.com/kb/en/how-to-quick ...
- Eclipse中使用GIT将已提交到本地的文件上传至远程仓库
GIT将已提交到本地的文件上传至远程仓库: 1. 右击项目——Team——Push to Upstream,即可将已保存在本地的文件上传推至GIT远程仓库.
- 几何入门合集 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个 ...
- hpu第六次周赛Problem F
Problem F Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- Problem F. Wiki with String
Problem F. Wiki with StringInput file: standard input Time limit: 1 secondOutput file: standard outp ...
- 实验12:Problem F: 求平均年龄
Home Web Board ProblemSet Standing Status Statistics Problem F: 求平均年龄 Problem F: 求平均年龄 Time Limit: ...
- 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 ...
随机推荐
- 如何使用UltraCompare对比两个文件夹内容差异
http://jingyan.baidu.com/article/cb5d6105e13599005c2fe0f8.html
- 正确地组织python项目的结构
统一的项目结构 写了不少python项目后, 越来越认识到python项目结构重要性. 不管项目是否要开源, 是否要提交pypi, 项目结构的一致性带来的好处还有很多: 多人合作开发大家都有个基本的g ...
- [设计模式] javascript 之 桥接模式
桥接模式说明 定义:分离抽象化与实现化,使之可以自由独立的变化: 说明:由于软件环境需求原因,使得类型抽象具有多种实现以自身变化定义等情况,这使得我们要分离抽象实现与具体实现,使得抽象化与实现化解耦, ...
- HDU 2010
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int Is_SXH(int num); int main() { int in1, ...
- C#深入浅出 关键字(一)
1.this this关键字用于指示当前对象“自己”,来看一个例子,了解什么时候需要用this class Star { String name; int age; public void SetIn ...
- Mac Pro 入门、遇到的问题、个性化设置 汇总
入门资料 入门一:Mac 基本用法 入门二:Mac 使用VMware Fusion虚拟机 入门三:Mac 使用brew安装软件 问题汇总 [问题1]如何复制文本? 一只手指头按下,另外一只手指头滑动选 ...
- RFM
前面博客中讲到的聚类,聚类是对客户的一些特征进行分群,属于描述,不涉及客户价值的判断,然而在营销中,其实第一步应该是搞清楚谁才是你的关键客户,哪些用户的价值较高,这就需要用到RFM模型.RFM模型是众 ...
- mysql基础面试
php面试题之五--MySQL数据库(基础部分) 五.MySQL数据库 mysql_num_rows() mysql_affected_rows() 这两个函数都作用于 mysql_query($qu ...
- 【转】phpcms基础内容
<?php 思路: 一.目前在企业中使用比较多的cms内容管理有如下几种: 1.dedecms 2.phpcms 二.我们选择学习v9版本的phpcms,主要有以下几点原因: 1.基于MVC模式 ...
- Java中Runnable和Thread
java中有两种实现多线程的方式:一种是继承Thread类,一种是实现Runnable接口. 1.java启动线程为什么使用start函数呢? 在JDK的安装路径下,src.zip是全部的java源程 ...