首页 > 其他分享 >山东大学数据结构实验七

山东大学数据结构实验七

时间:2023-04-25 21:12:57浏览次数:34  
标签:queueBack const int arrayQueue 实验 queueFront arrayLength 数据结构 山东大学

卡片游戏

tips:这个题还要参考,同学要加油啦~~

要求

  1. 创建队列类,使用数组描述的循环队列
  2. 实现卡片游戏

描述

假设桌上有一叠扑克牌,依次编号为1-n(从上至下)。当至少还有两张的时候,可以进行操作:把第一张牌扔掉,然后把新的第一张(原先扔掉的牌下方的那张牌,即第二张牌)放到整叠牌的最后。输入n,输出最后剩下的牌。

格式

输入

一个整数n,代表一开始卡片的总数。

输出

最后一张卡片的值。

样例

输入

100

输出

72

限制

1s, 64MB for each test case.

#include <iostream>

#include <algorithm>
using namespace std;

template<class T>
void changeLength1D(T *&a, int oldLength, int newLength) {
    T *temp = new T[newLength]; //新数组
    int number = min(oldLength, newLength); //需要复制的元素个数
    copy(a, a + number, temp);
    delete[] a; //释放老数组的内存空间
    a = temp;
}

template<class T>
class arrayQueue {
public:
    arrayQueue(int initialCapacity = 10);

    ~ arrayQueue() { delete[] queue; }

    bool empty() const { return queueFront == queueBack; }

    int size() const {
        return (arrayLength + queueBack
                - queueFront) % arrayLength;
    }
    /* LYZ */
    T &front() const; //返回队首元素
    T &back() const; // 返回队尾元素
    void pop(); //删除队首元素
    void push(const T &theElement); //元素插入到队尾
private:
    int queueFront; //与第一个元素在反时针方向上相差一个位置
    int queueBack; // 指向最后一个元素
    int arrayLength; // 队列数组容量
    T *queue; // 元素数组
};

template<class T>

arrayQueue<T>::arrayQueue(int initialCapacity) { // 构造函数
    arrayLength = initialCapacity;
    queue = new T[arrayLength];
    queueFront = 0,queueBack = 0;
}

template<class T>
T &arrayQueue<T>::front() const {//返回队首元素

    return queue[(queueFront + 1) % arrayLength];
}

template<class T>
T &arrayQueue<T>::back() const {// 返回队尾元素
    return queue[queueBack];
}

template<class T>
void arrayQueue<T>::pop() {// 删除队首元素
    queueFront = (queueFront + 1) % arrayLength;
    queue[queueFront].~T();
}

template<class T>
void arrayQueue<T>::push(const T &theElement) {// 把元素theElement添加到队列的尾部
//如果队列空间满,则加倍数组长度
    if ((queueBack + 1) % arrayLength == queueFront) {

        changeLength1D(queue, arrayLength, 2 * arrayLength);
        arrayLength *= 2;
    }
    queueBack = (queueBack + 1) % arrayLength;
    queue[queueBack] = theElement;
}


int main() {
   arrayQueue<int> Queue;
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        Queue.push(i);
    }
    while(Queue.size()>=2){
        Queue.pop();
        int flag=Queue.front();
        Queue.push(flag);
        Queue.pop();
    }
    int res=Queue.back();
    cout<<res<<endl;


    return 0;
}

标签:queueBack,const,int,arrayQueue,实验,queueFront,arrayLength,数据结构,山东大学
From: https://www.cnblogs.com/lyz103/p/17353884.html

相关文章

  • 实验三
    #1实验内容:1#12importrandom3print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('\n用集......
  • 山东大学数据结构实验六
    计算表达式tips:不要全文复制,会被查重哦注意因为精度问题,请使用double存数据。要求创建栈类,采用数组描述;计算数学表达式的值。输入数学表达式,输出表达式的计算结果。数学表达式由单个数字和运算符+、-、*、/、(、)构成,例如2+3*(4+5)-6/4。假定表达式输入格式合法。格式......
  • 山东大学数据结构实验一(2)
    题目描述现有一个有n个元素的序列\(a=[a_{1},a_{2},\cdots,a_{n}]\),定义其价值为\(\sum_{i=1}^{n}a_{i}\oplusi\)给出这样一个序列,求其所有排列的价值\(v_{i}\)的或\(v_{1}|v_{2}|\cdots|v_{n-1}|v_{n}\)其中\(|\)为位运算或操作,\(\oplus\)为位运算异......
  • 山东大学数据结构实验一(1)
    题目描述现有一个有\(n\)个元素的序列\(a=[a_1,a_2,\cdots,a_n]\),定义这个序列的价值为\(\sum_{i=1}^{n}i\timesa_i\)。空序列的价值为\(0\)。先给你一个长度为\(n\)的序列\(a\),求\(a\)中所有子集价值的异或和,要求子集中元素的相对位置保持不变。异或和:位运算的一种。如果a......
  • 实验三
    任务一实验源码运行结果截图任务二实验源码1lst=[55,92,88,79,96]23i=04whilei<len(lst):5print(lst[i],end='')6i+=17print()89foriinrange(len(lst)):10print(lst[i],end='')11print()1213forii......
  • 实验3 控制语句与组合数据类型应用编程
    实验任务1实验源码1importrandom23print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('......
  • 实验3
    1.实验任务1task1.py实验代码:importrandomprint('用列表储存随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合储存随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合储存随机整数:')s......
  • 实验3
    实验任务1:源码:importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set(......
  • 实验三
    实验任务一:实验源码:1importrandom23print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('\......
  • 实验三
    task1源代码importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()whilelen(......