约瑟夫环问题遇到难点了求大神指点
c语言吧
全部回复
仅看楼主
level 5
明月砚 楼主
总是出现“joeph.exe 中的 0x013a3a98 处有未经处理的异常: 0xC0000005: 读取位置 0x00000008 时发生访问冲突” 是不是我的指针用的有问题 或者循环链表建的有偏差 T^T
2013年03月30日 07点03分 1
level 5
明月砚 楼主
// joeph.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdlib.h"
typedef struct Cnode{
int person;//人员编号
int pw;//所持密码
struct Cnode *next;
}Cnode , *Clist;
//函数声明
void create(Clist &H , int n);
int kill(Clist &L, int pw0);
//主函数
void main(){
Clist H;//指向头结点的指针
Cnode *p , *q;//指向结点的指针
int n , pw0;
printf("Joeph约瑟夫环问题求解\n");
for(;1;){
printf("请输入人数(MAX<=30):");
scanf("%d",&n);
if(n>0||n<=30)
break;
else
printf("\n输入错误,请重新输入");
}
create(H , n);
while(1){
printf("\n请输入初始报数值(MAX<=30):");
scanf("%d",&pw0);
if(pw0>0||pw0<=30)
break;
else
printf("\n输入错误,请重新输入");
}
int i,j;
j=pw0;
for(i=1;i<=n;i++){
j=kill(H,j);
}
getchar();
getchar();
}
void create(Clist &H , int n){
Clist L;
Cnode *s, *last; //s指向新结点,last指向已建链表的最后结点
L=(Clist)malloc(sizeof(Cnode));
L->next==NULL;//建立头结点
last=L;
int m , i;
for(i=1;i<=n;i++){
s=(Clist)malloc(sizeof(Cnode));
s->person=i;
printf("请输入第%d个人对应的密码",i);
scanf("%d",&m);
s->pw=m;
s->next=NULL;
last->next=s;
}//初始化链表
H=L;
last->next=H->next;
}
int kill(Clist &L, int pw0){
Cnode *p, *pre; //pre指向p的前一结点
int a=0;//计数器
p=L->next; pre=NULL;
while (a!=pw0) {
pre=p;
p=p->next;
a++;
}
printf("KILL %d\n",p->person);
pw0=p->pw;
pre->next=p->next;
free(p);
return pw0;
}
2013年03月30日 07点03分 2
level 13
头指针..
2013年03月30日 08点03分 3
1