技巧(2)<queue>实现队列模拟

Card

Time Limit: 1000ms
Memory Limit: 32768KB
64-bit integer IO format: %I64d      Java class name: Main
Bearchild is playing a card game with himself. But first of all, he needs to shuffle the cards. His strategy is very simple: After putting all the cards into a single stack,
he takes out some cards in the middle, from the L-th to the R-th when counting from top to bottom, inclusive, and puts them on the top. He repeats this action again 
and again for N times, and then he regards his cards as shuffled.

Given L,R and N, and the initial card stack, can you tell us what will the card stack be like after getting shuffled?
 

Input

First line contains an integer T(1 <= T <= 1000), which is the test cases.
For each test case, first line contains 52 numbers(all numbers are distinct and between 1 and 52), which is the card number of the stack, from top to bottom. 
Then comes three numbers, they are N, L and R as described. (0<=N<=109, 1<=L<=R<=52)
 

Output

For each test case, output "Case #X:", X is the test number, followed by 52 numbers, which is the card number from the top to bottom.Note that you should output one and only 
one blank before every number.
 

Sample Input

1
13 2 10 50 1 28 37 32 30 46 19 47 33 41 24 52 27 42 49 18 9 48 23 35 31 8 7 12 6 5 3 22 43 36 51 40 26 4 44 
17 39 38 15 14 25 16 29 20 21 45 11 34
902908328 38 50

Sample Output

Case #1: 26 4 44 17 39 38 15 14 25 16 29 20 21 45 13 2 10 50 1 28 37 32 30 46 19 47 33 41 24 52 27 42 49 18 
9 48 23 35 31 8 7 12 6 5 3 22 43 36 51 40 11 34
#include<cstdio>
#include<queue>
using namespace std;
int a[60];
int mod(int x,int y){
    return y==0 ? x : mod(y,x%y);
}
int main(){
    int T,cas = 0;
    scanf("%d",&T);
    while(T--){
        for(int i = 1; i <= 52; i++)
            scanf("%d",&a[i]);
        int n,l,r;
        scanf("%d%d%d",&n,&l,&r);
        queue<int>myque;
        while(!myque.empty())myque.pop();
        for(int i = r; i >= 1; i--)//倒着放入队列;
            myque.push(a[i]);
        n %= r/mod(r-l+1,r);//先使r和r-l+1互质,求出r;再n取余;
     while(n--){ for(int j = 0; j <= r-l; j++) myque.push(myque.front()),myque.pop();//模拟过程; } for(int i = r; i >= 1; i --)//倒着输出; a[i] = myque.front(),myque.pop(); printf("Case #%d:",++cas); for(int i = 1; i <= 52; i++) printf(" %d",a[i]); printf("\n"); } return 0; }

 

posted @ 2015-08-28 19:03  Tobu  阅读(111)  评论(0)    收藏  举报