2106 Problem F Shuffling Along
来源:互联网 发布:淘宝产品资质认证 编辑:程序博客网 时间:2024/05/20 23:32
题目描述
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.
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;}
0 0
- 2106 Problem F Shuffling Along
- Problem F
- ProBlem F
- Problem F
- Problem F
- Problem F
- Problem F
- problem F
- Problem f
- Problem F
- Problem F
- Problem F
- Problem F
- Problem-F
- Problem F
- Problem F
- Problem-F
- Problem F
- Activity的生命周期
- pfx文件
- java RandomAccessFile
- ant安装使用指南
- Android问题集锦之四十八:Error:duplicate files during packaging of APK
- 2106 Problem F Shuffling Along
- Eclipse使用Maven创建Spring MVC项目(包含实例)以及部署到tomcat服务器
- STL 中的通用容器
- NDK编译出现undefined reference to错误
- mysql 中LIMIT 分页查询
- Maven创建项目: Failed to execute goal org.apache.maven.plugin( mvn archetype:create)
- iOS9新增关键字和一些新特性
- CodeForces About arry 暑期小练习 数字的统计
- ArcGIS Server Manager 登陆失效