目标:一群猴子,编号是1,2,3,,,,m,这群猴子按照1-m排序做一圈。从第一只开始数,没到第n个,该猴子离开圈子,到最后,剩下一个猴子就是猴王。
代码:
#include <iostream>
#include "kudetouwenjian.h"
using namespace std;
int main()
{
linknode *L;
A e=0;
int m,n,c;
cin>>m>>n;
A a[m];
A b[m];
for(int i=0;i<m;i++)
a[i]=i+1;
xunhuanlianbiao(L,a,m);
if(n==1)
{
cout<<m<<endl;
return 0;
}
houzi(L,n,m);
return 0;
}
#include <iostream>
#include <malloc.h>
#include "kudetouwenjian.h"
using namespace std;
void houzi(linknode *&L,int n,int m)
{
linknode *p=L->next;
for(int i=0;i<m-1;i++)
{
for(int a=0;a<n-1;a++)
{
L=p;
p=L->next;
}
L->next=p->next;
free(p);
p=L->next;
}
cout<<L->shuju<<endl;
}
void xunhuanlianbiao(linknode *&L,A a[],int n)
{
linknode *s,*r;
L=(linknode *)malloc(sizeof(linknode));
r=L;
for(int i=0;i<n;i++)
{
s=(linknode *)malloc(sizeof(linknode));
s->shuju=a[i];
r->next=s;
r=s;
}
r->next=L->next;
}
void toucha(linknode *&L,A a[],int n)
{
linknode *s;
L=(linknode *)malloc(sizeof(linknode));
L->next=NULL;
for(int i=0;i<n;i++)
{
s=(linknode *)malloc(sizeof(linknode));
s->shuju=a[i];
s->next=L->next;
L->next=s;
}
}
void shuchu(linknode *L)
{
linknode *p=L->next;
while(p!=NULL)
{
cout<<p->shuju<<" ";
p=p->next;
}
cout<<endl;
}
void weicha(linknode *&L,A a[],int n)
{
linknode *s,*r;
L=(linknode *)malloc(sizeof(linknode));
r=L;
for(int i=0;i<n;i++)
{
s=(linknode *)malloc(sizeof(linknode));
s->shuju=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void chushihua(linknode *&L)
{
L=(linknode *)malloc(sizeof(linknode));
L->next=NULL;
}
void cuihui(linknode *&L)
{
linknode * pre=L,* p=L->next;
while(p!=NULL)
{
free(pre);
pre=p;
p=pre->next;
}
free(pre);
L->next=NULL;
}
int panduanshifouweikong(linknode *L)
{
return(L->next==NULL);
}
int changdu(linknode *L)
{
int e=0;
linknode *p=L->next;
while(p!=NULL)
{
e++;
p=p->next;
}
return(e);
}
int shunxuchazhao(linknode * L,int i,A &e)
{
int j=1;
linknode *p=L->next;
if(i<=0)j=j;;
while(j<i)
{
j++;
p=p->next;
}
if(p==NULL)
j=j;
else
{
e=p->shuju;
return(e);
}
}
int chazhaodange(linknode *L,A e)
{
int j=1;
linknode *p=L->next;
if(p==NULL)
return 0;
while(p!=NULL&&e!=p->shuju)
{
p=p->next;
j++;
}
if(p==NULL)
return 0;
else
return(1);
}
bool charudange(linknode *&L,int i,A e)
{
int j=0;
linknode * p=L,* s;
if(i<=0)return false;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
s=(linknode *)malloc(sizeof(linknode));
s->shuju=e;
s->next=p->next;
p->next=s;
}
}
#ifndef KUDETOUWENJIAN_H_INCLUDED
#define KUDETOUWENJIAN_H_INCLUDED
#include <iostream>
typedef int A;
typedef struct lianbiao
{
A shuju;
struct lianbiao *next;
}linknode;
void toucha(linknode *&L,A a[],int n);
void shuchu(linknode *L);
void weicha(linknode *&L,A a[],int n);
void chushihua(linknode *&L);
void cuihui(linknode *&L);
int panduanshifouweikong(linknode *L);
int changdu(linknode *L);
int shunxuchazhao(linknode *L,int i,A &e);
int chazhaodange(linknode *L,A e);
bool charudange(linknode *&L,int i,A e);
void xunhuanlianbiao(linknode *&L,A a[],int n);
void houzi(linknode *&L,int n,int m);
#endif // KUDETOUWENJIAN_H_INCLUDED
运行图:
![](https://img-blog.csdn.net/20171022001956150?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfNDAwOTI3MzQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
心得:
这次算是一次循环链表的应用题。我把它想的太难了,用链表的删除就行了。