大虾们帮忙看下我的程序哪里有问题

来源:百度知道 编辑:UC知道 时间:2024/06/14 10:42:35
我是新手,下面这个程序是自己写的用链表解决约瑟夫问题,不知道为什么编译运行都正常,一输完两个值就会显示“出现问题需要关闭”的提示,请大家帮忙看看,我是新手,谢谢大家了

#include<iostream>
#define NULL 0

struct jos
{
int num;
jos *next;
};

int n;
struct jos *creat( )
{
jos *head;
jos *le,*ls;
ls=new jos;
ls->num=1;
head=NULL;
le=ls;
int k=1;
while(k<=n)
{
if(head==NULL)
{
head=ls;
}
else
{
le->next=ls;
}
le=ls;
ls=new jos;
ls->num=k;
k++;
}
le->next=NULL;
delete ls;
return head;
}
void main()
{
int m,s=1;
jos *call;
jos *count;
std::cout<<"Please input the totle number and the calling number"<<std::endl;
std::cin>>n>>m;
creat();
call=creat();
while(call->next!=call)
{
while(s<=m-1)
{

我刚费劲为你重写了一个,看看吧,该有很大收获
#include<iostream>
#define NULL 0

typedef struct JOS
{
int num;
JOS *next;
}JOS;

JOS *creat(int n)
{
JOS *head;
JOS *le,*ls;
ls=new JOS;
head=ls;
le=ls;
le->num=1;
for(int k=2;k<=n;k++)
{
ls=new JOS;
le->next=ls;
le=ls;
le->num=k;
}
le->next=head;
return head;
}

void main()
{
int m,n,s=1;
JOS *call;
JOS *count,*tmp;
std::cout<<"Please input the totle number and the calling number"<<std::endl;
std::cin>>n>>m;
call=creat(n);
while(call->next!=call)
{
while(s<m-1)
{
call=call->next;
s++;
}
count=call->next;
call->next=count->next;
std::cout<<"The killed number is"<<