题目如下:
桌上有叠牌,从第一张牌(即位于顶面的牌)开始从上往下依次编号为1~n。
当至少还剩两张牌时进行以下操作:把第一张牌扔掉,然后把新的第一张放一整叠牌的
最后。输入n,输出每次扔掉的牌,以及最后剩下的牌。
样例输入:7
样例输出:1 3 5 74 2 6
Queue.h
<pre name="code" class="cpp">/* Queue Declaration */
/*
filename: Queue.h
function: The declaration of LinkQueue and CirQueue
author: Yin Kailin
E-mail: 2414225774@qq.com
version: 1.0
*/
#define TRUE 1
#define FALSE 0
#define INIT_SUCC 0
#define INIT_FAIL 2
#define INSR_SUCC 0
#define INSR_FAIL 3
#define DELE_SUCC 0
#define DELE_FAIL 4
#define TRV_FAIL 7
#define TRV_SUCC 0
#define GET_SUCC 0
#define GET_FAIL 5
#define DES_FAIL 6
#define DES_SUCC 0
typedef int ElemType;
typedef int status;
typedef struct QNode
{
ElemType elem;
struct QNode * next;
}QNode, * QNPtr;
typedef struct
{
QNPtr head;
QNPtr tail;
int elemLen;
}LinkQueue, * LkQPtr;
/* The declarations of functions of LinkQueue */
//initialize a LinkQueue
status InitQueue(LkQPtr *);
//insert an elem into Queue
status EnQueue(LkQPtr *, ElemType *);
//delete an elem from Queue
status DeQueue(LkQPtr *, ElemType *);
//When Queue is empty return 1
int IsEmpty(LkQPtr *);
//get the head elem not delete it
status GetHead(LkQPtr *, ElemType *);
//get the tail elem not delete it
status GetTail(LkQPtr *, ElemType *);
//return the length of the elem in the queue
int GetLen(LkQPtr *);
//clear out the queue
status ClrQueue(LkQPtr *);
//destroy the queue
status DesQueue(LkQPtr *);
//traverse the queue elem by elem and
//operate each elem with the specified
//function
status TrvQueue(LkQPtr *, void (*)(QNPtr));
//traverse each element in Queue
void vInt(QNPtr);
void vChar(QNPtr);
void vDbl(QNPtr);
void vStr(QNPtr);
Queue.c
/* Source Code*/
/*
filename: Queue.c
function: The declaration of LinkQueue and CirQueue
author: Yin Kailin
E-mail: 2414225774@qq.com
version: 1.0
*/
#include "Queue.h"
#include <stdlib.h>
#include <stdio.h>
/*
PreCond: an uninitial pointer.
PostCond: make that pointer to
a struct of LinkQueue.
*/
status InitQueue(LkQPtr * rawQ)
{
(*rawQ) = (LkQPtr)malloc(sizeof(LinkQueue));
if( NULL == (*rawQ) )
return INIT_FAIL;
(*rawQ)->head = NULL;
(*rawQ)->tail = NULL;
(*rawQ)->elemLen = 0;
return INIT_SUCC;
}
/*
PreCond: initQ must be initialized and
insElem not null.
PostCond: insert the element of type
ElemType(*insElem) into LinkQueue
pointed by (*initQ),If insert su-
ccessfully then return INSR_SUCC
or return INSR_FAIL.
*/
status EnQueue(LkQPtr * initQ, ElemType * insElem)
{
QNPtr tmptr = (QNPtr)malloc(sizeof(QNode));
if( NULL == tmptr ) return INSR_FAIL;
//initialize tmptr
tmptr->elem = *insElem;
tmptr->next = NULL;
//insert tmptr into queue
if( (*initQ)->head == NULL )
{
(*initQ)->head = tmptr;
(*initQ)->tail = tmptr;
(*initQ)->elemLen++;
}
else
{
(*initQ)->tail->next = tmptr;
(*initQ)->tail = tmptr;
(*initQ)->elemLen++;
}
return INSR_SUCC;
}
/*
PreCond: the same as the one above.
Postcond: Delete the first elem in
the LinkQueue and return
DELE_SUCCC if success or
return DELE_FAIL.
*/
status DeQueue(LkQPtr * insQ, ElemType * rsvElem)
{
QNPtr tmptr = NULL;
if( NULL == (*insQ)->head || (*insQ)->elemLen <= 0)
return DELE_FAIL;
tmptr = (*insQ)->head;
(*insQ)->head = (*insQ)->head->next;
(*insQ)->elemLen--;
if( rsvElem ) *rsvElem = tmptr->elem;
free(tmptr);
if( NULL == (*insQ)->head )
(*insQ)->head = (*insQ)->tail = NULL;
return DELE_SUCC;
}
/* return TRUE if empty or FALSE */
int IsEmpty(LkQPtr * Q)
{
return (*Q)->elemLen <= 0 ? TRUE : FALSE;
}
/* get the first elem in the queue */
status GetHead(LkQPtr * Q, ElemType * rsvElem)
{
if( NULL == (*Q)->head || (*Q)->elemLen <= 0 )
return GET_FAIL;
*rsvElem = (*Q)->head->elem;
return GET_SUCC;
}
/*get the last elem in the queue */
status GetTail(LkQPtr * Q, ElemType * rsvElem)
{
if( NULL == (*Q)->tail || (*Q)->elemLen <= 0 )
return GET_FAIL;
*rsvElem = (*Q)->tail->elem;
return GET_SUCC;
}
/* return the number of elements in the queue */
int GetLen(LkQPtr * Q)
{
return (*Q)->elemLen >= 0 ? (*Q)->elemLen : -1;
}
/* release the mem occupied by the queue */
status DesQueue(LkQPtr * Q)
{
QNPtr tmptr = NULL;
if( NULL == (*Q)->head || (*Q)->elemLen <= 0 )
return DES_FAIL;
tmptr = (*Q)->head;
do
{
tmptr = (*Q)->head;
(*Q)->head = (*Q)->head->next;
free(tmptr);
}while((*Q)->head != (*Q)->tail);
free((*Q)->head);
(*Q)->head = (*Q)->tail = tmptr = NULL;
(*Q)->elemLen = 0;
return DES_SUCC;
}
/* traverse all over the queue with
each element operated by 'visit'
*/
status TrvQueue(LkQPtr * Q, void (*visit)(QNPtr node))
{
QNPtr tmptr = NULL;
if( NULL == (*Q)->head || (*Q)->elemLen <= 0)
return TRV_FAIL;
tmptr = (*Q)->head;
while(tmptr != NULL)
{
visit(tmptr);
tmptr = tmptr->next;
}
return TRV_SUCC;
}
/* visit functions */
void vInt(QNPtr node)
{
printf("%d ", node->elem);
return;
}
void vDbl(QNPtr node)
{
printf("%.3f", node->elem);
return;
}
void vChar(QNPtr node)
{
printf("%c ", node->elem);
return;
}
void vStr(QNPtr node)
{
printf("%s ", node->elem);
return;
}
CardsGame.c
/* Cards Game Program */
#include "Queue.h"
#include <stdio.h>
int main()
{
LkQPtr queue = NULL;
int nCards, i, trwAwy, newFrst;
scanf("%d", &nCards);
if( InitQueue(&queue) )
return 1;
for(i = 1; i <= nCards; i++)
EnQueue(&queue, &i);
//Game rules
while(GetLen(&queue) >= 2)
{
DeQueue(&queue, &trwAwy);
DeQueue(&queue, &newFrst);
EnQueue(&queue, &newFrst);
printf("%d ", trwAwy);
}
if(GetLen(&queue) == 1)
{
DeQueue(&queue, &trwAwy);
printf("%d\n", trwAwy);
}
DesQueue(&queue);
return 0;
}