这是一个JOSEPHUS问题
我编的代码如下,可运行时总是会把头一个元素默认为0,请大家帮忙看一下.可能是构造函数有问题,但就是找不出#include<iostream>
using namespace std;
#define N 1500
class node{
public: int data;
node* next;
node(){//int a;
//data=a;
next=NULL;
}
node(int a,node* p=NULL)
{
data=a;
next=p;
}
};
class joe:public node{
public:
node* fence;
node* head;
int size;
/*joe(){
head=fence=NULL;
}*/
joe()
{
head=fence=new node(data,head);
fence->next=head;
//size=1;
}
bool append(const int& a)
{//fence=fence->next=new node(a.data,head);
fence->next=new node(a,head);
fence=fence->next;
size++;
return true;
}
/*bool remove(node& it){
if(size==0)
return 0;
it.data=head->data;
node* ltemp=head;
head=ltemp->next;
delete ltemp;
size--;
return 1;
}*/
bool removehead(){
if(size==0)
return false;
node* ltemp=head;
head=head->next;
fence->next=head;
delete ltemp;
size--;
return true;
}
};
int main(){
int m,n,a[N];
cin>>m>>n;
joe b;
for(int i=0;i<=m-1;i++)
{cin>>a[i];
b.append(a[i]);
}
if(n==1)
for(int i=0;i<=m-1;i++)
/*{cout<<b.head->data;
b.head=b.head->next;
}*/
cout<<a[i]<<" ";
else while( m)
{
for(int i=0;i<=n-2;i++)
{ b.head=b.head->next;
b.fence=b.fence->next;
}
//b.remove(node(b.head->data,b.head));
cout<<b.head->data<<" ";
b.removehead();
m--;
}
return 0;
}