贤鱼的刷题日常(数据结构队列学习)-2406:Card Stacking--题目详解

简介: >🏆今日学习目标:🍀例题讲解2406:Card Stacking✅创作者:贤鱼⏰预计时间:25分钟
🏆今日学习目标:
🍀例题讲解2406:Card Stacking
✅创作者:贤鱼
⏰预计时间:25分钟

请添加图片描述
@TOC

题目

总时间限制: 20000ms 单个测试点时间限制: 1000ms 内存限制: 65536kB
描述
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.
输入

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

样例输入
3 9 2
样例输出
3
7
8
提示
INPUT DETAILS:

Bessie is playing cards with two cow friends and a deck of 9 cards. She must place two cards on the bottom of the deck each time she deals one.

OUTPUT DETAILS:

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.

思路

看不懂题目的百度翻译一下谢谢
这个题目的要求就是n个人,k个牌,每次发牌完毕将顶部的p个牌放到末尾
我们直接循环
如果刚好等于本人,计入当前牌号
不是本人就出队

以上两个操作随便一个以后,循环出队入队完成放牌操作,具体看代码,一看就会!!

AC代码

#include<cmath>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n,k,p;
queue<int>q;
int bs[100005];
int ecnt=0;
int main(){
    cin>>n>>k>>p;
    for(int i=1;i<=k;i++){
        q.push(i);
    }
    for(int i=1;i<=k;i++){
        if(i%n==0){
            bs[++ecnt]=q.front();
        }
        q.pop();
        for(int j=1;j<=p;j++){
            int m=q.front();
            q.push(m);
            q.pop();
        }
    }
    sort(bs+1,bs+1+ecnt);
    for(int i=1;i<=ecnt;i++){
        cout<<bs[i]<<endl;
    }
}

请添加图片描述

相关文章
|
5月前
|
前端开发 Java
java实现队列数据结构代码详解
本文详细解析了Java中队列数据结构的实现,包括队列的基本概念、应用场景及代码实现。队列是一种遵循“先进先出”原则的线性结构,支持在队尾插入和队头删除操作。文章介绍了顺序队列与链式队列,并重点分析了循环队列的实现方式以解决溢出问题。通过具体代码示例(如`enqueue`入队和`dequeue`出队),展示了队列的操作逻辑,帮助读者深入理解其工作机制。
150 1
232.用栈实现队列,225. 用队列实现栈
在232题中,通过两个栈(`stIn`和`stOut`)模拟队列的先入先出(FIFO)行为。`push`操作将元素压入`stIn`,`pop`和`peek`操作则通过将`stIn`的元素转移到`stOut`来实现队列的顺序访问。 225题则是利用单个队列(`que`)模拟栈的后入先出(LIFO)特性。通过多次调整队列头部元素的位置,确保弹出顺序符合栈的要求。`top`操作直接返回队列尾部元素,`empty`判断队列是否为空。 两题均仅使用基础数据结构操作,展示了栈与队列之间的转换逻辑。
|
6月前
|
算法 数据可视化 开发者
为什么要学习数据结构与算法
今天,我向大家介绍一门非常重要的课程——《数据结构与算法》。这门课不仅是计算机学科的核心,更是每一位开发者从“小白”迈向“高手”的必经之路。
为什么要学习数据结构与算法
|
8月前
|
存储 C语言 C++
【C++数据结构——栈与队列】顺序栈的基本运算(头歌实践教学平台习题)【合集】
本关任务:编写一个程序实现顺序栈的基本运算。开始你的任务吧,祝你成功!​ 相关知识 初始化栈 销毁栈 判断栈是否为空 进栈 出栈 取栈顶元素 1.初始化栈 概念:初始化栈是为栈的使用做准备,包括分配内存空间(如果是动态分配)和设置栈的初始状态。栈有顺序栈和链式栈两种常见形式。对于顺序栈,通常需要定义一个数组来存储栈元素,并设置一个变量来记录栈顶位置;对于链式栈,需要定义节点结构,包含数据域和指针域,同时初始化栈顶指针。 示例(顺序栈): 以下是一个简单的顺序栈初始化示例,假设用C语言实现,栈中存储
329 77
|
7月前
|
算法 调度 C++
STL——栈和队列和优先队列
通过以上对栈、队列和优先队列的详细解释和示例,希望能帮助读者更好地理解和应用这些重要的数据结构。
148 11
|
7月前
|
DataX
☀☀☀☀☀☀☀有关栈和队列应用的oj题讲解☼☼☼☼☼☼☼
### 简介 本文介绍了三种数据结构的实现方法:用两个队列实现栈、用两个栈实现队列以及设计循环队列。具体思路如下: 1. **用两个队列实现栈**: - 插入元素时,选择非空队列进行插入。 - 移除栈顶元素时,将非空队列中的元素依次转移到另一个队列,直到只剩下一个元素,然后弹出该元素。 - 判空条件为两个队列均为空。 2. **用两个栈实现队列**: - 插入元素时,选择非空栈进行插入。 - 移除队首元素时,将非空栈中的元素依次转移到另一个栈,再将这些元素重新放回原栈以保持顺序。 - 判空条件为两个栈均为空。
|
8月前
|
存储 C++ 索引
【C++数据结构——栈与队列】环形队列的基本运算(头歌实践教学平台习题)【合集】
【数据结构——栈与队列】环形队列的基本运算(头歌实践教学平台习题)【合集】初始化队列、销毁队列、判断队列是否为空、进队列、出队列等。本关任务:编写一个程序实现环形队列的基本运算。(6)出队列序列:yzopq2*(5)依次进队列元素:opq2*(6)出队列序列:bcdef。(2)依次进队列元素:abc。(5)依次进队列元素:def。(2)依次进队列元素:xyz。开始你的任务吧,祝你成功!(4)出队一个元素a。(4)出队一个元素x。
236 13
【C++数据结构——栈与队列】环形队列的基本运算(头歌实践教学平台习题)【合集】
|
8月前
|
C++
【C++数据结构——栈和队列】括号配对(头歌实践教学平台习题)【合集】
【数据结构——栈和队列】括号配对(头歌实践教学平台习题)【合集】(1)遇到左括号:进栈Push()(2)遇到右括号:若栈顶元素为左括号,则出栈Pop();否则返回false。(3)当遍历表达式结束,且栈为空时,则返回true,否则返回false。本关任务:编写一个程序利用栈判断左、右圆括号是否配对。为了完成本关任务,你需要掌握:栈对括号的处理。(1)遇到左括号:进栈Push()开始你的任务吧,祝你成功!测试输入:(()))
182 7
|
10月前
|
C语言
【数据结构】栈和队列(c语言实现)(附源码)
本文介绍了栈和队列两种数据结构。栈是一种只能在一端进行插入和删除操作的线性表,遵循“先进后出”原则;队列则在一端插入、另一端删除,遵循“先进先出”原则。文章详细讲解了栈和队列的结构定义、方法声明及实现,并提供了完整的代码示例。栈和队列在实际应用中非常广泛,如二叉树的层序遍历和快速排序的非递归实现等。
855 9
|
10月前
|
存储 算法
非递归实现后序遍历时,如何避免栈溢出?
后序遍历的递归实现和非递归实现各有优缺点,在实际应用中需要根据具体的问题需求、二叉树的特点以及性能和空间的限制等因素来选择合适的实现方式。
213 59