A : 卡片游戏
要求
- 创建队列类,使用数组描述的循环队列
- 实现卡片游戏
描述
假设桌上有一叠扑克牌,依次编号为1-n(从上至下)。当至少还有两张的时候,可以进行操作:把第一张牌扔掉,然后把新的第一张(原先扔掉的牌下方的那张牌,即第二张牌)放到整叠牌的最后。输入n,输出最后剩下的牌。
格式
输入
一个整数n,代表一开始卡片的总数。
输出
最后一张卡片的值。
样例
输入
100
输出
72
以0代替n,实现要求(因为用0代替n并不会对实验进行影响)
#include <iostream>
using namespace std;
template<class T>
class arrayQueue{
public:
arrayQueue(int initialCapacity = 10); // 构造函数
~arrayQueue(){delete[] queue;} // 析构函数
void push(const T& theElement); // 插入
void pop(); // 删除
// bool empty(){return queueFront = queueBack;}
int size()const{return (arrayLength + queueBack - queueFront) % arrayLength;}
T &front(){ // 返回队首元素
return queue[queueFront + 1 % arrayLength];
}
T &back(){ // 返回队尾元素
return queue[queueBack];
}
T output(){
while(size() != 1){
pop();
push(front());
pop();
}
return front();
}
// void change1(){
// T* newQueue = new T[2*arrayLength]; // 分配空间
// int start = (queueFront + 1) % arrayLength;
// if(start < 2){ // 没有形成环
// copy(queue + start, queue + start + arrayLength - 1, newQueue);
// }
// else{ // 形成环
// copy(queue + start, queue + arrayLength, newQueue);
// copy(queue, queue + queueBack + 1, newQueue + arrayLength - start);
// }
// // 重新设置队首和队尾元素
// queueFront = 2 * arrayLength - 1;
// queueBack = arrayLength - 2;
// arrayLength = arrayLength * 2;
// delete[] queue;
// queue = newQueue;
// }
private:
int arrayLength; // 长度
int queueFront; // 头
int queueBack; // 尾
T *queue; // 队列
};
// 构造函数
template<class T>
arrayQueue<T>::arrayQueue(int initialCapacity){
arrayLength = initialCapacity;
queue = new T[arrayLength];
queueFront = 0;
queueBack = queueFront;
for(int i = 0; i < initialCapacity; i++){
queue[i] = i;
// queueBack = (queueBack + 1) % arrayLength;
// if((queueBack + 1) % arrayLength == queueFront){
// change1();
// }
}
}
// 插入函数
template<class T>
void arrayQueue<T>::push(const T& theElement){
// if((queueBack + 1) % arrayLength == queueFront){
// change1();
// }
queueBack = (queueBack + 1) % arrayLength;
queue[queueBack] = theElement;
}
// 删除函数
template<class T>
void arrayQueue<T>::pop(){
queueFront = (queueFront+1) % arrayLength;
queue[queueFront].~T();
}
int main(){
int n;
cin >> n;
arrayQueue<int> q1(n);
cout << q1.output() << endl;
return 0;
}
正常使用1-n实现
#include <iostream>
using namespace std;
template<class T>
class arrayQueue{
public:
arrayQueue(int initialCapacity = 10); // 构造函数
~arrayQueue(){delete[] queue;} // 析构函数
void push(const T& theElement); // 插入
void pop(); // 删除
// bool empty(){return queueFront = queueBack;}
int size()const{return (arrayLength + queueBack - queueFront) % arrayLength;}
T &front(){ // 返回队首元素
return queue[queueFront + 1 % arrayLength];
}
T &back(){ // 返回队尾元素
return queue[queueBack];
}
void change1(){
T* newQueue = new T[2*arrayLength]; // 分配空间
int start = (queueFront + 1) % arrayLength;
if(start < 2){ // 没有形成环
copy(queue + start, queue + start + arrayLength - 1, newQueue);
}
else{ // 形成环
copy(queue + start, queue + arrayLength, newQueue);
copy(queue, queue + queueBack + 1, newQueue + arrayLength - start);
}
// 重新设置队首和队尾元素
queueFront = 2 * arrayLength - 1;
queueBack = arrayLength - 2;
arrayLength = arrayLength * 2;
delete[] queue;
queue = newQueue;
}
private:
int arrayLength; // 长度
int queueFront; // 头
int queueBack; // 尾
T *queue; // 队列
};
// 构造函数
template<class T>
arrayQueue<T>::arrayQueue(int initialCapacity){
arrayLength = initialCapacity;
queue = new T[arrayLength];
queueFront = 0;
queueBack = queueFront;
for(int i = 0; i < initialCapacity; i++){
queue[i] = i;
queueBack = (queueBack + 1) % arrayLength;
if((queueBack + 1) % arrayLength == queueFront){
change1();
}
}
}
// 插入函数
template<class T>
void arrayQueue<T>::push(const T& theElement){
if((queueBack + 1) % arrayLength == queueFront){
change1();
}
queueBack = (queueBack + 1) % arrayLength;
queue[queueBack] = theElement;
}
// 删除函数
template<class T>
void arrayQueue<T>::pop(){
queueFront = (queueFront+1) % arrayLength;
queue[queueFront].~T();
}
int main(){
int n;
cin >> n;
arrayQueue<int> q1(n);
while(q1.size() != 1){
q1.pop();
q1.push(q1.front());
q1.pop();
}
cout << q1.back() << endl;
return 0;
}