网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
12月26日漏签0天
c语言吧 关注:801,653贴子:4,374,352
  • 看贴

  • 图片

  • 吧主推荐

  • 视频

  • 游戏

  • 13回复贴,共1页
<<返回c语言吧
>0< 加载中...

约瑟夫问题的升级版,判断总是出错

  • 只看楼主
  • 收藏

  • 回复
  • 知表不言
  • 帕秋莉糕
    12
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
这段代码由约瑟夫问题更改而来,只不过每次间隔个数是随机的(由被删除的结构中data数值决定),可是问题就是出现在倒数第十一行的m,把判断条件改成定值3程序就能正常完成,如果直接这样的代码编译,会出现segmentation fault ,pointer being freed was not allocated等等错误,这个该怎么修改才能正常啊
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define random(x) (rand()%x)
#define N 41 //人数
#define M 23 //报数上限
struct List
{
int num;
int data;
struct List *next;
};
struct List *CreatList(int n);
void PrintList(struct List *head);
void Suicide(struct List *head);
int main(void)
{
srand((int)time(0));
struct List *head = CreatList(N);
//PrintList(head);
Suicide(head);
return 0;
}
struct List *CreatList(int n)
{
struct List *temp;
struct List *tail = (struct List *)malloc(sizeof(struct List));
struct List *head = tail;
tail->num = n;
for(int i = 1; i <= n; i++)
{
temp = (struct List *)malloc(sizeof(struct List));
temp->num = n - i;
do
{
temp->data = random(M);
}while(!(temp->data));
temp->next = head;
head = temp;
}
do
{
tail->data = random(M);
}while(!(tail->data));
tail->next = head->next;
return head;
}
void PrintList(struct List *head)
{
struct List *temp = head->next;
do
{
printf("%d\t", temp->num);
printf("%d\n", temp->data);
temp = temp->next;
}while(temp != head->next);
}
void Suicide(struct List *head)
{
int count = 0;
int m = head->next->data;
struct List *temp = head;
struct List *prior = NULL;
while(1)
{
count++;
prior = temp;
temp = temp->next;
if(temp->next == temp)
{
printf("号码为%2d的人自杀了!\t",temp->num);
m = temp->data;
printf("他的报数是%2d\n",m);
prior->next = temp->next;
free(temp);
break;
}
if(count == m)
{
printf("号码为%2d的人自杀了!\t",temp->num);
m = temp->data;
printf("他的报数是%2d\n",m);
prior->next = temp->next;
free(temp);
count = 0;
}
}
}


  • 知表不言
  • 帕秋莉糕
    12
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
编译能通过,都是在运行时出现错误的


2025-12-26 05:15:54
广告
不感兴趣
开通SVIP免广告
  • 归海一刀
  • 大能力者
    8
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
当head->next被free(游戏进行到一定程度第一个人肯定会死的),tail->next仍然指向head->next而没有重新赋值,然后就像
这样


  • 归海一刀
  • 大能力者
    8
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
还有一个,也是在最后一个函数里。如果count==m成立成功执行if下面的语句并成功free掉temp并进入下一轮循环,那么问题来了,prior=temp是不是会出错呢。。


  • 归海一刀
  • 大能力者
    8
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
试下这段代码,看有什么bug
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define random(x) (rand()%x)
#define N 41 //人数
#define M 23 //报数上限
struct List
{
int flag; //标志变量,1为存活,0则死亡
int num;
int data;
struct List *next;
};
struct List *CreatList(int n);
void PrintList(struct List *head);
void Suicide(struct List *head);
int main(void)
{
srand((int)time(0));
struct List *head = CreatList(N);
PrintList(head);
Suicide(head);
return 0;
}
struct List *CreatList(int n)
{
struct List *temp;
struct List *tail = (struct List *)malloc(sizeof(struct List));
struct List *head = tail;
tail->num = n;
tail->flag=1;
for(int i = 1; i <= n; i++)
{
temp = (struct List *)malloc(sizeof(struct List));
temp->num = n - i;
temp->flag=1;
do
{
temp->data = random(M);
}while(!(temp->data));
temp->next = head;
head = temp;
}
do
{
tail->data = random(M);
}while(!(tail->data));
tail->next = head->next;
return head;
}
void PrintList(struct List *head)
{
struct List *temp = head->next;
do
{
printf("%d\t", temp->flag);
printf("%d\t", temp->num);
printf("%d\n", temp->data);
temp = temp->next;
}while(temp != head->next);
}
void Suicide(struct List *head)
{
int count = 0,i=0;
int m = head->next->data;
struct List *temp = head;
//struct List *p;
while(i<N)
{
temp=temp->next;
if(temp->flag==0)
;
else
{
count++;
if(count == m)
{
printf("号码为%2d的人自杀了!\t",temp->num);
m = temp->data;
printf("他的报数是%2d\n",m);
temp->flag=0;
count = 0;
i++;
}
}
}
}


  • 归海一刀
  • 大能力者
    8
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
我跑出来没毛病


  • 知表不言
  • 帕秋莉糕
    12
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define random(x) (rand()%x)
#define N 41 //人数
#define M 23 //报数上限
struct List
{
int num;
int data;
struct List *next;
};
struct List *CreatList(int n);
void PrintList(struct List *head);
void Suicide(struct List **head);
int main(void)
{
srand((int)time(0));
struct List *head = CreatList(N);
//PrintList(head);
Suicide(&head);
return 0;
}
struct List *CreatList(int n)
{
struct List *temp;
struct List *tail = (struct List *)malloc(sizeof(struct List));
struct List *head = tail;
tail->num = n;
for(int i = 1; i <= n; i++)
{
temp = (struct List *)malloc(sizeof(struct List));
temp->num = n - i;
do
{
temp->data = random(M);
}while(!(temp->data));
temp->next = head;
head = temp;
}
do
{
tail->data = random(M);
}while(!(tail->data));
tail->next = head->next;
return head;
}
void PrintList(struct List *head)
{
struct List *temp = head->next;
do
{
printf("%d\t", temp->num);
printf("%d\n", temp->data);
temp = temp->next;
}while(temp != head->next);
}
void Suicide(struct List **head)
{
int count = 0;
int m = (*head)->next->data;
struct List *temp = *head;
struct List *prior = NULL;
while(1)
{
count++;
prior = temp;
temp = temp->next;
if(temp->next == temp)
{
printf("号码为%2d的人自杀了!\t",temp->num);
m = temp->data;
printf("他的报数是%2d\n",m);
prior->next = temp->next;
if(temp == *head)
{
*head = temp->next;
}
free(temp);
break;
}
else if(count == m)
{
printf("号码为%2d的人自杀了!\t",temp->num);
m = temp->data;
printf("他的报数是%2d\n",m);
prior->next = temp->next;
if(temp == *head)
{
*head = temp->next;
}
free(temp);
temp = prior;
count = 0;
}
}
}


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 13回复贴,共1页
<<返回c语言吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示