题目描述
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.
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.
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 ;
}