带密码的约瑟夫问题,为什么N取较大值时通过不了

带密码的约瑟夫问题

带密码的约瑟夫问题:编号为1,2,......,n的n个人按照顺时针方向围坐一圈,每个人有自己的编号(正整数)、姓名和密码(正整数)三个数据项。一开始任选一个正整数作为报数上限值,从第一个人开始顺时针方向自1开始报数,报到m时停止报数。报m 的人出列,将他的密码作为新的m值,从他在顺时针方向的下一个人开始重新报数,如此下去,直到所有人全部出队为止。设计一个程序来求出出队顺序。

#include

#include
typedef struct People {
int number;
char name[1000];
int key;
}People;
typedef struct StackNode {
People data;
struct StackNode* next;
} StackNode, * LinkStack;
//建立循环单链表
void CreateClistF(LinkStack &L, People P[], int N) {
LinkStack p, r;
//L = (StackNode*)malloc(sizeof(StackNode));
L = new StackNode;
L->next = NULL;
r = L;
for (int i = 0; i < N; i++)
{
p = new StackNode;
scanf("%d,%[^,],%d", &P[i].number, P[i].name, &P[i].key);
p->data=P[i];
p->next=NULL;
r->next = p;
r = p;
}
p->next=L;
//printf("%d,%s,%d",p->data.number,p->data.name,p->data.key);
}
void Seeklist(int M,LinkStack &L,int N)
{
People Pe[N];
LinkStack t;
t=L;
int count=N;
for(int j=0;j
{
for(int i=0;i
{
if(t->next!=L)
{
t=t->next;
}
else
{
t=t->next->next;
}
}
M=t->next->data.key;
if(count==1)
{
if(N!=1)
printf("%d,%s,%d\n", t ->next->data.number,t ->next->data.name, t ->next->data.key);
else
printf("%d,%s,%d\n",t->data.number,t->data.name,t->data.key);
}
else
{
printf("%d,%s,%d\n",t->next->data.number,t->next->data.name,t->next->data.key);
if(t->next->next==L)
{
t->next=t->next->next->next;
}
else
t->next=t->next->next;
count--;
}
}
}
int main()
{
int N;
scanf("%d", &N);
StackNode* L;
People P[N];
CreateClistF(L,P,N);
int M;
scanf("%d",&M);
Seeklist(M,L,N);
return 0;
}

N一般值,稍微偏大的情况

答案错误

我的解答思路和尝试过的方法

#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>

using namespace std;

typedef struct MyList
{
    int num;
    char name[10];
    int code;
    struct MyList *next;
}MyList,*List;

int main()
{
    List L;
    L=(List)malloc(sizeof(MyList));
    L->next=NULL;
    int n,ciallo1,ciallo3;
    char ciallo2[10];
    List a=L,b;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        b=(List)malloc(sizeof(MyList));
        scanf("%d,%[^,],%d", &ciallo1, ciallo2, &ciallo3);
        b->num=ciallo1;
        strcpy(b->name,ciallo2);
        b->code=ciallo3;
        b->next=NULL;
        a->next=b;
        a = b;
    }
    b->next=L;

    int m=0;
    cin>>m;
    int count=0;

    List c=L;
    while(count<n)
    {

        for(int i=0;i<m;i++)
        {
            c=c->next;
            while(c==L||c->num==0)
            {
                c=c->next;
            }
        }
        cout<<c->num<<","<<c->name<<","<<c->code;
        if(count!=n-1)
        {
            cout<<"\n";
        }
        c->num=0;
        m=c->code;
        count++;
    }

    return 0;
}


有一个问题,在seeklist函数里面 链表的头结点为什么一直都是跳过的,如果刚好是这个头结点要出队是不是错了?
已经是一个循环链表了,不用对那个L特殊处理 只要判断 t = t->next 就是最后一个。然后数到m就删除,在重新数,count啥的变量应该不需要,代码写的有点复杂

您好,您的代码,最好格式化显示下,您这样直接复制粘贴实在没法看。。