题解 | Card Stacking-牛客假日团队赛6A题

A-Card Stacking_牛客假日团队赛6

https://ac.nowcoder.com/acm/contest/993/A

题目描述 

Bessie is playing a card game with her N-1 cow friends using a deck with K ; 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 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.

输入描述:

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

输出描述:

* 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.

示例1

输入
3 9 2
输出
3
7
8
说明
Bessie should put the "good" cards in positions 3, 7, and 8. The cards will be dealt as follows; the card numbers are "position in original deck":
Card Deck         P1      P2    Bessie
Initial configuration           1 2 3 4 5 6 7 8 9  - - -   - - -   - - -
Deal top card [1] to Player 1   2 3 4 5 6 7 8 9    1 - -   - - -   - - -
Top card to bottom (#1 of 2)    3 4 5 6 7 8 9 2    1 - -   - - -   - - -
Top card to bottom (#2 of 2)    4 5 6 7 8 9 2 3    1 - -   - - -   - - -
Deal top card [4] to Player 2   5 6 7 8 9 2 3      1 - -   4 - -   - - -
Top card to bottom (#1 of 2)    6 7 8 9 2 3 5      1 - -   4 - -   - - -
Top card to bottom (#2 of 2)    7 8 9 2 3 5 6      1 - -   4 - -   - - -
Deal top card [7] to Bessie     8 9 2 3 5 6        1 - -   4 - -   7 - -
Top card to bottom (#1 of 2)    9 2 3 5 6 8        1 - -   4 - -   7 - -
Top card to bottom (#2 of 2)    2 3 5 6 8 9        1 - -   4 - -   7 - -
Deal top card [2] to Player 1   3 5 6 8 9          1 2 -   4 - -   7 - -
Top card to bottom (#1 of 2)    5 6 8 9 3          1 2 -   4 - -   7 - -
Top card to bottom (#2 of 2)    6 8 9 3 5          1 2 -   4 - -   7 - -
Deal top card [6] to Player 2   8 9 3 5            1 2 -   4 6 -   7 - -
Top card to bottom (#1 of 2)    9 3 5 8            1 2 -   4 6 -   7 - -
Top card to bottom (#2 of 2)    3 5 8 9            1 2 -   4 6 -   7 - -
Deal top card [3] to Bessie     5 8 9              1 2 -   4 6 -   7 3 -
Top card to bottom (#1 of 2)    8 9 5              1 2 -   4 6 -   7 3 -
Top card to bottom (#2 of 2)    9 5 8              1 2 -   4 6 -   7 3 -
Deal top card [9] to Player 1   5 8                1 2 9   4 6 -   7 3 -
Top card to bottom (#1 of 2)    8 5                1 2 9   4 6 -   7 3 -
Top card to bottom (#2 of 2)    5 8                1 2 9   4 6 -   7 3 -
Deal top card [5] to Player 2   8                  1 2 9   4 6 5   7 3 -
Top card to bottom (#1 of 2)    8                  1 2 9   4 6 5   7 3 -
Top card to bottom (#1 of 2)    8                  1 2 9   4 6 5   7 3 -
Deal top card [8] to Bessie     -                  1 2 9   4 6 5   7 3 8
Bessie will end up with the "good cards" that have been placed in positions 3, 7, and 8 in the original deck.

解答

这题的意思就是一个叫Bessie的人和他n-1个朋友玩发牌游戏,一共有k张牌,有m=k/n张好牌(k/m张坏牌),从右开始发,也就是说Bessie是最后一个拿到牌的,每发一张牌都需要将最上面连续的p张牌放到下面,Bessie想要获胜,所以请你找出应该把好牌放在那些位置(就是找出Bessie获得牌的位置)并升序输出,在名义上,顶部卡是#1卡,下一张卡是#2,依此类推。
解题思路:
用一个queue数组模仿队列,一个头下标,一个尾下标,然后每发1张牌就将p张牌放到数组最后面,循环发出n-1张牌,下一张要发的牌就是好牌,一共要发k/n张好牌,下面是AC代码:
#include"iostream"
#include"algorithm"

using namespace std;

const int MAX = 10000007; 
int queue[MAX];//模仿队列数组 
int n,k,p;//人数,牌数,需要放到下面的牌数 
int good_cards[MAX];//存放好牌 
int size;//好牌数组的下标 

int main()
{
	cin >> n >> k >> p;
	for(int i=1;i<=k;i++)//初始化队列
		queue[i] = i;
	int top=1;//队列的头 
	int bot=k+1;//队列的尾 
	for(int q=1;q<=k/n;q++)//一共要发出k/n张好牌 
	{
		for(int j=0;j<n-1;j++)//从右边开始发牌,发n-1次 
		{	
			top++;
			for(int i=0;i<p;i++)//将p张牌依次放到最后 
				queue[bot++]=queue[top++];
		}
		good_cards[size++] = queue[top++];//存储好牌 
		for(int i=0;i<p;i++)//将p张牌依次放到最后
			queue[bot++]=queue[top++];	
	}
	sort(good_cards,good_cards+size);//升序 
	for(int i=0;i<size;i++)
		cout << good_cards[i] << endl;
	return 0;
}

来源:Friends233
全部评论

相关推荐

06-13 17:33
门头沟学院 Java
顺序不记了,大致顺序是这样的,有的相同知识点写分开了1.基本数据类型2.基本数据类型和包装类型的区别3.==和equals区别4.ArrayList与LinkedList区别5.hashmap底层原理,put操作时会发生什么6.说出几种树型数据结构7.B树和B+树区别8.jvm加载类机制9.线程池核心参数10.创建线程池的几种方式11.callable与runnable区别12.线程池怎么回收线程13.redis三剑客14.布隆过滤器原理,不要背八股,说说真正使用时遇到了问题没有(我说没有,不知道该怎么回答了)15.堆的内存结构16.自己在写项目时有没有遇见过oom,如何处理,不要背八股,根据真实经验,我说不会17.redis死锁怎么办,watchdog机制如何发现是否锁过期18.如何避免redis红锁19.一个表性别与年龄如何加索引20.自己的项目的QPS怎么测的,有没有真正遇到大数量表21.说一说泛型22.springboot自动装配原理23.springmvc与springboot区别24.aop使用过嘛?动态代理与静态代理区别25.spring循环依赖怎么解决26.你说用过es,es如何分片,怎么存的数据,1000万条数据怎么写入库中27.你说用limit,那么在数据量大之后,如何优化28.rabbitmq如何批次发送,批量读取,答了延迟队列和线程池,都不对29.计网知不知道smtp协议,不知道写了对不对,完全听懵了30.springcloud知道嘛?只是了解反问1.做什么的?短信服务,信息量能到千万级2.对我的建议,基础不错,但是不要只背八股,多去实际开发中理解。面试官人不错,虽然没露脸,但是中间会引导我回答问题,不会的也只是说对我要求没那么高。面完问我在济宁生活有没有困难,最快什么时候到,让人事给我聊薪资了。下午人事打电话,问我27届的会不会跑路,还在想办法如何使我不跑路,不想扣我薪资等。之后我再联系吧,还挺想去的😭,我真不跑路哥😢附一张河科大幽默大专图,科大就是大专罢了
查看30道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务