#include <iostream>
using namespace std;
typedef struct tagNode
{
tagNode *pNext;
int data;
}Node;
//创建有n个结点的循环链表
Node *CreateLoopList(int n)
{
Node *head = NULL;
Node *tail = NULL;
Node *temp = NULL;
//创建头结点
head = tail = new Node;
head->pNext = NULL;
head->data = 0;
for (int i = 1; i < n; ++i)
{
temp = new Node;
temp->pNext = NULL;
temp->data = i;
tail->pNext = temp;
tail = temp;
}
tail->pNext = head;
return head;
}
//返回最后一个元素(从当前位置开始数,第num个出列,依次类推)
int LastNumber(Node *head, int num)
{
Node *pPre = head;
Node *pCur = head;
int n = 1;
while (pCur->pNext != pCur)
{
while (n < num)
{
pPre = pCur;
pCur = pCur->pNext;
++n;
}
n = 1;
pPre->pNext = pCur->pNext;
delete pCur;
pCur = pPre->pNext;
}
return pCur->data;
}
int main()
{
Node *nd = new Node;
nd = CreateLoopList(3);
int n = LastNumber(nd, 3);
cout<<n<<endl;
return 1;
}
第18题: 题目:n个数字(0,1,„,n-1)形成一个圆圈,从数字0开始, 每次从这个圆圈中删除第m个数字(第一个为当前数字本身,第二个为当前数字的下一个数字)。 当一个数字删除后,从被删除数字的下
最新推荐文章于 2021-02-28 02:41:59 发布