—————————————————————————
#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
=
0
i
<
iNum
i++)
{
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
s,
int
iNum)
{
int
i
=
0
Ring_t
*pstTemp
=
*pstRing,
*pst
=
NULL
while
(pstTemp->pnext
!=
*pstRing)
{
pstTemp
=
pstTemp->pnext
}
for
(i
=
1
i
<
s
i++)
{
pstTemp
=
pstTemp->pnext
}
while
(pstTemp->pnext
!=
pstTemp)
{
for
(i
=
1
i
<
iNum
i++)
{
pstTemp
=
pstTemp->pnext
}
pst
=
pstTemp->pnext
pstTemp->pnext
=
pst->pnext
printf("
%d
",
pst->Num)
free
(pst)
pst
=
NULL
}
printf("
%d\n",
pstTemp->Num)
free
(pstTemp)
pstTemp
=
NULL
return
1
}
int
main()
{
Ring_t
*pstRing
=
NULL
int
n
=
0,
s
=
0,
m
=
0
InitRing(&pstRing)
printf("请输入n,
s
和
m的值:")
scanf("%d
%d
%d",
&n,
&s,
&m)
CreateRing(&pstRing,
n)
printf("出列顺序为:
")
DeleteNode(&pstRing,
s,
m)
return
1
}
正好之前写过基础的约瑟夫环,稍作修改就可以满足你的题目
#include <stdio.h>#include <stdlib.h>
typedef struct _node {
int id
int key
struct _node *next
} Linklist
int main() {
int n, m
scanf("%d %d", &n, &m)
int i, count = 0
Linklist *head = (Linklist*)malloc(sizeof(Linklist)), *tail = head
head->id = 1
scanf("%d", &head->key)
head->next = head
for(i = 2 i <= n i++) {
Linklist *p = (Linklist*)malloc(sizeof(Linklist))
p->id = i
scanf("%d", &p->key)
p->next = head
tail->next = p
tail = p
}
while(head != tail) {
if(++count % m) {
tail = head
} else {
m = head->key
count = 0
printf("%d ", head->id)
tail->next = head->next
free(head)
}
head = tail->next
}
printf("%d\n", head->id)
free(head)
return 0
}