POJ-3629

Card Stacking
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3927 Accepted: 1541

Description

Bessie is playing a card game with her N-1 (2 ≤ N ≤ 100) cow friends using a deck with K (N ≤ K ≤ 100,000; K is a multiple of N) cards. The deck contains M = K/N "good" cards and K-M "bad" cards. Bessie is the dealer and, naturally, wants to deal herself all of the "good" cards. She loves winning.

Her friends suspect that she will cheat, though, so they devise a dealing system in an attempt to prevent Bessie from cheating. They tell her to deal as follows:

1. Start by dealing the card on the top of the deck to the cow to her right

2. Every time she deals a card, she must place the next P (1 ≤ P ≤ 10) cards on the bottom of the deck; and

3. Continue dealing in this manner to each player sequentially in a counterclockwise manner

Bessie, desperate to win, asks you to help her figure out where she should put the "good" cards so that she gets all of them. Notationally, the top card is card #1, next card is #2, and so on.

Input

* Line 1: Three space-separated integers: NK, and P

Output

* Lines 1..M: Positions from top in ascending order in which Bessie should place "good" cards, such that when dealt, Bessie will obtain all good cards.

Sample Input

3 9 2

Sample Output

3
7
8

题意:

Bessie跟朋友玩牌,总共n人k张牌,k是n的整数倍。k张牌中共有k/n张好牌,Bessie想要作弊得到所有好牌。可Bessie的同伴为了防止Bessie作弊而设定了如下规则:

1.每次发牌从牌顶发,从Bessie右手边第一个人开始。

2.每发出一张牌就要将接下来的p张牌依次放至牌底。

按照这个方法发完所有牌。即使是这样,Bessie依然想要拿到所有好牌,请你帮她计算出将好牌放在哪些位置可以实现Bessie的愿望。

用队列可以很方便的解决这个问题,将所有k张牌入队,发牌即为出队,过牌即出队后再入队,每循环到Bessie时将队首元素记录下来,即为好牌位置。

 

附AC代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 
 8 const int MAX=50500;
 9 
10 int ans[MAX];
11 int main(){
12     int n,k,p;
13     queue<int> q;//定义队列 
14     memset(ans,0,sizeof(ans));
15     while(~scanf("%d %d %d",&n,&k,&p)){
16         for(int i=1;i<=k;i++){//入队 
17             q.push(i);
18         }
19         int temp=1,sum=0;
20         while(1){
21             if(temp++%n==0){//temp从1开始每循环至Bessie处则为好牌 
22                 ans[sum++]=q.front();
23                 if(sum==k/n){//好牌放完则退出循环 
24                     break;
25                 }
26             }
27             q.pop();//发出的牌出队 
28             for(int i=0;i<p;i++){//其他牌移到牌底 
29                 q.push(q.front());
30                 q.pop();
31             } 
32         }
33         sort(ans,ans+sum);
34         for(int i=0;i<sum;i++){
35             printf("%d\n",ans[i]);
36         }
37     }
38     return 0;
39 }

 

转载于:https://www.cnblogs.com/Kiven5197/p/5523548.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值