注册 登录
编程论坛 C++教室

老鼠求生问题

spring123 发布于 2007-04-25 22:59, 643 次点击
有只猫抓了N只老鼠,抓来的老鼠把它排成一圈,数到第三只老鼠,吃掉,依次数下去,第三只都被吃掉,最后一只放生,若你是此老鼠,该站在哪个位置?puts(老鼠只数)
5 回复
#2
qypwxy2007-04-25 23:54
编之~~~~~~~~
链表~~~~~~~~
#3
煲仔粥2007-04-26 00:41
7
#4
zhangbo851282007-05-08 10:46
7
#5
潇湘夜雨2007-05-12 19:32

自己试了一下,但感觉还有可以改进的地方
#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;
}

#6
nuciewth2007-05-12 20:54
约瑟复环问题.
1