约瑟夫环问题的程序运行出错!!!求高手指导!!急急急!!!

hkping420 2011-09-21 07:54:45
问题:编译可以通过,也可以运行,但是运行过后就会“弹出一个窗口:约瑟夫环程序已停止了工作”!求解,求修改!!请高手帮忙!!!谢谢!!
代码如下:



#include<iostream>
using namespace std;
#define OK 1


typedef struct DuLNode{
int num;
int pw;
struct DuLNode *next;
}DuLNode;DuLNode *L;;




void CreateList_L(int n)
{
DuLNode *p, *q;
L=(DuLNode*)calloc(1, sizeof(DuLNode));
L->num=1;
p=L;
cout<<n<<"个人的密码:";
cin>>p->pw;
for(int i =2; i<=n;i++){
q=(DuLNode*)malloc( sizeof(DuLNode));
q->num=i;
cin>>q->pw;
p->next=q;
p=q;
}
p->next=L;
}


int GetElem_L(int i){
DuLNode *p,*q;
p=L;
while(i>=1){
for(int j=2;j<=i;j++)
{
q=p;
p=p->next;
}
q->next=p->next;
i=p->pw;
cout<<p->num<<" ";
free(p);
p=q->next;
}
return OK;
}


void main()
{
int i,n;
cout<<"输入创建人数:";
cin>>n;
CreateList_L(n);
cout<<"输入极限值:";
cin>>i;
cout<<"输出序号:";
GetElem_L(i);
}


...全文
95 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hackbuteer1 2011-09-21
  • 打赏
  • 举报
回复
http://blog.csdn.net/hackbuteer1/article/details/6581454
这里有的,楼主可以参考一下
赵4老师 2011-09-21
  • 打赏
  • 举报
回复
//假设有n个人团团围做,从第1个人开始数数,数到第m个人时候,第m个人出列,
//然后继续从1开始数数,数到第m个人退出
#include <stdio.h>
#include <conio.h>
int i,k,t;
int n,m;
static char f[1001];//0该座位未出圈,1该座位已出圈
void main() {
while (1) {
printf("Input n m(1000>=n>=m>=1):");
fflush(stdout);
rewind(stdin);
if (2==scanf("%d%d",&n,&m)) {
if (1000>=n && n>=m && m>=1) break;
}
}
t=0;//已出圈总人数
i=1;//座位编号
k=1;//当前要数的数
while (1) {
if (0==f[i]) {
if (m==k) {
t++;
f[i]=1;
printf("%3d ",i);
if (0==t%10) printf("\n");
if (t>=n) break;
}
k++;if (k>m) k=1;
}
i++;if (i>n) i=1;
}
cprintf("Press any key ...");
getch();
}
logiciel 2011-09-21
  • 打赏
  • 举报
回复
int GetElem_L(int i){
DuLNode *p,*q;
p=L;
while(i>=1){
for(int j=2;j<=i;j++)
{
q=p;
p=p->next;
}
q->next=p->next;
i=p->pw;
cout<<p->num<<" ";
free(p);
if (p==q) break;//加
p=q->next;
}
return OK;
}

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧