自己试了一下,但感觉还有可以改进的地方
#include<iostream>
#include<conio.h>
using namespace std;
struct mouse{
int logo;
struct mouse *next;
};
int Last_mouse( int N ){
/*创建链表*/
struct mouse *head,*m,*n;
int i=1;
head=m=new struct mouse;
m->logo=i;
m->next=NULL;
while(i<N){
i++;
n=new struct mouse;
n->logo=i;
n->next=NULL;
m->next=n;
m=n;
}
n->next=head;
/*吃掉数到3的老鼠*/
m=head;n=m->next;
for(int k=1;;k++){
if(k!=2){ //n的起始logo值是2,故只需数一次
m=n;
n=n->next;
}
else {
m->next=n->next;
m=m->next;
n=m->next;
k=0;
}
if(n->logo == m->logo)
return m->logo;
}
}
int main()
{
int Num;
cout<<"Input the total number of the mouse :"<<endl;
cin>>Num;
cout<<"The last mouse's logo is :"<<Last_mouse(Num);
getch();
return 0;
}