HDU 4350 card

本文介绍了一种通过重复将特定区间的卡片移至顶部来模拟洗牌过程的算法,并提供了详细的实现思路与代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Card

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 134    Accepted Submission(s): 82


Problem Description
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 befor 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
 

题意不难理解 ,有52张扑克叠放在一起 ,给你一个L,R,也就是要抽出的区间然后放在最前面 ,这样洗牌N次。一个看到这道题的那个循环次数,我就觉得他肯定有循环节,看他循环多少次能回到最初的位置,也就是代码中的lcd;

用次数Mod循环节就是剩下要洗牌的次数,我的time--就是把第一次要抽出的牌抽(后面称为 “大部队”)了出来放在最前,然后把R个牌按每部分(R-L+1)分成几部分(假设为X),当然最后一部分不一定等于前面的,然后可以得知每洗X次“大部队”可以回到前面,但是会有extre张在“大部队”前面。然后extre可以mod,R,也就是实际最后有多少张在“大部队”前面。然后根据这个数再去分类判断就可以得知洗完牌以后的TOP的那张牌的位置,然后依次输出。


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int a[55];

int gcd( int a , int b )
{
    return b ? gcd( b , a%b ) : a;    
}

int main( )
{
    int cas,k,l,r;
    scanf("%d",&cas);
    for( int q=1 ; q<=cas ; q++ )
    {
         for( int i=1 ; i<=52 ; i++ )     
              scanf("%d",&a[i]);
         scanf("%d%d%d",&k,&l,&r);
         int dis,t,lcd;
         dis = r - l + 1;
         t = gcd( dis , r );
         printf("t=%d\n",t);
         lcd = dis*r/t;
         printf("lcd=%d\n",lcd);
         int time = k % lcd;
         printf("%d\n",time);
         time--;
         int yushu,zhangshu,many,extre;
         many = r / dis;
         extre = dis - ( r % dis );
         if( extre )
             many++; 
         yushu = time % many;
         time /= many;
         zhangshu = time * extre;
         while( zhangshu - r >= 0 )
                zhangshu -= r;
         zhangshu %= dis;
         printf("%d\n",zhangshu);
         zhangshu = dis - zhangshu;
         int begin = (l - zhangshu) - (dis*(yushu-1));
         printf("begin=%d\n",begin);
         for( int i=begin ; i<=r ; i++ )
              printf("%d ",a[i]);
         for( int i=1 ; i<begin ; i++ )
              printf("%d ",a[i]);
         for( int i=r+1 ; i<=52 ; i++ )
              printf( i == 52 ? "%d\n" : "%d ",a[i] );
    }
    return 0;    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值