You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.3 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
#pragma region C++实现的循环双链表通用模板
struct DoubleNode {
int element; //节点存储信息可以根据需要修改!
DoubleNode *previous;
DoubleNode *next;
};
//创建头节点
DoubleNode *CreateLists(int X) {
DoubleNode *Lists = new DoubleNode;
Lists->element = X;
Lists->previous = Lists->next = Lists; //单节点链表形成自循环
return Lists;
}
//删除循环双链表
void DeleteLists(DoubleNode *Lists) {
assert(Lists);
DoubleNode *P = Lists->next, *temp;
Lists->next = P->previous = NULL;
while (P != Lists) {
temp = P->next;
P->next = temp->previous = NULL;
delete P;
P = temp;
}
delete Lists;
}
//在双循环链表中查找值X
DoubleNode *Find(int X, DoubleNode *Lists) {
assert(Lists);
DoubleNode *P = Lists;
do {
if (P->element == X)
return P;
else
P = P->next;
} while (P != Lists);
return NULL;
}
//按值X进行删除
void Delete(int X, DoubleNode *Lists) {
DoubleNode *temp = Find(X, Lists); //如果没找到X则temp=NULL
if (temp) {
temp->previous->next = temp->next;
temp->next->previous = temp->previous;
temp->previous = temp->next = NULL;
delete temp;
}
}
//在循环双链表中插入数值X
void Insert(DoubleNode *P, int X) {
DoubleNode *tempX = new DoubleNode;
tempX->element = X;
tempX->previous = P;
tempX->next = P->next;
P->next->previous = tempX;
P->next = tempX;
}
//输出循环双链表
void OutputLists(DoubleNode *Lists) {
DoubleNode *P = Lists;
do {
cout << P->element << " ";
P = P->next;
} while (P != Lists);
cout << endl;
}
//获取节点的数量
int GetNumofNodes(DoubleNode *Lists) {
int count = 1;
DoubleNode *P = Lists->next;
while (P != Lists) {
count++;
P = P->next;
}
return count;
}
//循环双链表两节点互换
void DoExchange(DoubleNode *A, DoubleNode *B) {
if (A == B)
return;
DoubleNode *PreA = A->previous;
DoubleNode *PostA = A->next;
DoubleNode *PreB = B->previous;
DoubleNode *PostB = B->next;
//假如循环双链表只包含A、B两个节点
if (PreA == PostA && PostA == B)
return;
//考虑相邻情况
if (PostA == B) {
PreA->next = B;
B->previous = PreA;
B->next = A;
A->previous = B;
A->next = PostB;
PostB->previous = A;
return;
}
if (PostB == A) {
PreB->next = A;
A->previous = PreB;
A->next = B;
B->previous = A;
B->next = PostA;
PostA->previous = B;
return;
}
//再考虑其它一般情况
PreA->next = B;
B->previous = PreA;
B->next = PostA;
PostA->previous = B;
PreB->next = A;
A->previous = PreB;
A->next = PostB;
PostB->previous = A;
}
#pragma endregion C++实现的循环双链表通用模板
int main() {
int n, k, p;
cin >> n >> k >> p;
//创建循环双链表head
DoubleNode *head = CreateLists(1);
DoubleNode *P = head; //游标
//将头节点后的n-1个数字插入
for (int i = 1; i < n; i++) {
//插入数据
Insert(P, i + 1);
//移动游标
P = P->next;
}
//牌的初始化
queue<int> pai;
for (int i = 0; i < k; ++i) {
pai.push(i + 1);
}
vector<int> v1;
int count = 0;
//模拟发牌
while (!pai.empty()) {
//1、 将最上面的牌发给小明右手边的人
int c = pai.front();
pai.pop();
count++;
if (count % n == 0) {
//小明人拿的牌
v1.push_back(c);
}
//2、每发完一张牌他必须将接下来的 P 张牌1≤P≤10一张一张地依次移到最后放在牌堆的底部。
for (int i = 0; i < p; ++i) {
c = pai.front();
pai.push(c);
pai.pop();
}
}
//排序
sort(v1.begin(), v1.end());
for (int i = 0; i < v1.size(); ++i) {
cout << v1[i] << endl;
}
return 0;
}