m0_74451357 2022-10-22 09:52 采纳率: 100%
浏览 57
已结题

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

带密码的约瑟夫问题

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

#include<stdio.h>

#include<stdlib.h>
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<N;j++)
{
for(int i=0;i<M-1;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一般值,稍微偏大的情况

答案错误

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

3条回答 默认 最新

  • myh12356 2022-10-23 12:04
    关注
    
    #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;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月4日
  • 已采纳回答 10月27日
  • 赞助了问题酬金15元 10月23日
  • 创建了问题 10月22日

悬赏问题

  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?