|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请教dalao约瑟夫问题,我想用循环链表实现,但是要求人数超过10的时候,要十个一行地输出(这是需要解决的问题),该怎么办呢
代码和网上的差不多,只有少量修改,请大佬帮助
#include <stdlib.h>
#include <stdio.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int ElemType;
typedef int Status;
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode;
typedef LNode *LinkList;
LinkList Joseph;
Status InitList_Sq(LinkList L, int n) {
int i;
LinkList pNew, pTail;
for (i = n; i >= 1; --i) {
pNew = (LinkList) malloc(sizeof(LNode));
if (NULL == pNew) exit(OVERFLOW);
pNew->data = i;
pNew->next = L;
L = pNew;
if (i == n)
pTail = pNew;
}
pTail->next = L;
Joseph = L;
return OK;
}
Status DoJoseph(LinkList L, int k, int n, int m) {
LinkList p, pNext;
int i;
if (!InitList_Sq(L, n)) exit(OVERFLOW);
p = Joseph;
for (i = 0; i < k - 1; ++i)
p = p->next;
while (p) {
for (i = 0; i < m - 2; ++i)
p = p->next;
pNext = p->next;
printf("%d ", pNext->data);
if (p->next == p)
p = NULL;
else {
p->next = pNext->next;
p = p->next;
free(pNext);
}
}
L = NULL;
}
int main(){
int m,n,k;
LinkList L;
L=NULL;
scanf("%d%d%d",&n, &k, &m);
if(n<1||m<1||k<1)
{
printf("n,m,k must bigger than 0.\n");
}
else if(k>n)
{
printf("k should not bigger than n.\n");
}
else
{
InitList_Sq(L,n);
DoJoseph(L,k,n,m);
}
return 0;
}
|
-
-
|