#includeusing namespace std;
struct Node
{
int m_nValue;
Node *m_pNext;
};
//输入x,数到num的数退出
int joseph(int x,int num)
{
if(x<=0)
return -1;
if(x == 1)
return 1;
Node *p,*head;
head = p = (Node *)malloc(sizeof Node);
p->m_nValue = 1;
for(int i = 2; i <= x; i ++)
{
Node *pNext = (Node *)malloc(sizeof Node);
p->m_pNext = pNext;
pNext->m_nValue = i;
printf("%d\n",i);
p = pNext;
}
p->m_pNext = head;
p = head;
for(int i = 1;i <15;i++)
{
printf("%8d \n",p->m_nValue);
p = p->m_pNext;
}
int count = 0, size = x;
p = head;
while(size > 1)
{
++count ;
if(count == num)
{
count = 0;
p->m_nValue = p->m_pNext->m_nValue;
Node *ptemp = p->m_pNext;
p->m_pNext = p->m_pNext->m_pNext;
free(ptemp);
--size;
}
else
p = p->m_pNext;
}
printf("the last one%d address %d\n",p->m_nValue,p);
//printf("%d size\n",size);
free(p);
}