#include<stdio.h>
#include<malloc.h>
typedef struct tagRing_t
{
int Num
struct tagRing_t *pnext
}Ring_t
int InitRing(Ring_t **pstRing)
{
*pstRing = (Ring_t *)malloc(sizeof(Ring_t))
if (NULL == *pstRing)
{
return 0
}
(*pstRing)->Num = 1
(*pstRing)->pnext = *pstRing
return 1
}
int CreateRing(Ring_t **pstRing, int iNum)
{
int i = 0
Ring_t *pstTemp = NULL, *pst = NULL
if (iNum <1)
{
return 0
}
pstTemp = *pstRing
for (i = 0i <iNumi++)
{
if (0 == i)
{
(*pstRing)->Num = 1
}
else
{
pst = (Ring_t *)malloc(sizeof(Ring_t))
if (NULL == pst)
{
return 0
}
pst->Num = i + 1
pstTemp->pnext = pst
pst->pnext = *pstRing
pstTemp = pstTemp->pnext
}
}
return 1
}
int DeleteNode(Ring_t **pstRing, int iNum)
{
int i = 0
Ring_t *pstTemp = *pstRing, *pst = NULL
while (pstTemp->pnext != pstTemp)
{
for (i = 2i <iNumi++)
{
pstTemp = pstTemp->pnext
}
pst = pstTemp->pnext
pstTemp->pnext = pst->pnext
printf(" %d ", pst->Num)
free (pst)
pst = NULL
pstTemp = pstTemp->pnext
}
printf(" %d\n", pstTemp->Num)
free (pstTemp)
pstTemp = NULL
return 1
}
int main()
{
Ring_t *pstRing = NULL
int iNum = 0, iCount = 0
InitRing(&pstRing)
printf("请输入人数和报数号:")
scanf("%d %d", &iNum, &iCount)
CreateRing(&pstRing, iNum)
printf("出列顺序为: ")
DeleteNode(&pstRing, iCount)
return 1
}
#include<stdio.h>int main() {
int num[50],n,m,i,j
int len,start = 0,counter = 1
printf("总数 报数\n")
scanf("%d%d",&n,&m)
if(n < 0 || n > 50 ) n = 50
if(m < 1 || m > n) m = n/2
printf("人数:%d,报数:%d\n",n,m)
for(i = 0 i < n ++i) num[i] = i + 1 // 预填
len = n // len保留队列中现有人数
while(len) {
if(counter == m) {
printf("%d ",num[start])
for(j = start j < len - 1 ++j)
num[j] = num[j + 1]
--len
counter = 1
}
else {
++counter
++start
start %= len
}
}
printf("\n")
return 0
}