有只猫抓了N只老鼠,抓来的老鼠把它排成一圈,数到第三只老鼠,吃掉,依次数下去,第三只都被吃掉,最后一只放生,若你是此老鼠,该站在哪个位置?puts(老鼠只数)
#include "stdio.h"
#include "malloc.h"
typedef struct Node
{
int data;
struct Node *next;
}mouNode;
void main()
{
int i,mouNumber;
mouNode *head,*pre,*cur,*p;
printf("请输入老鼠的个数:");
scanf("%d",&mouNumber);
head=(mouNode *)malloc(sizeof(mouNode));
head ->data = 1;
head->next = head;
cur=pre=head;
for(i=1;i<mouNumber;i++)
{
p=(mouNode *)malloc(sizeof(mouNode));
p->data=i+1;
cur->next=p;
cur=p;
p->next=head;
}
cur = head;
while(head->next->next != head)
{
pre = cur;
p = pre->next->next;
if(p == head ) head = head ->next;
pre->next->next = p->next;
free(p);
cur = cur->next->next;
}
printf("当老鼠在第%d个位置时它会被放生",head->next->data);
}
第一次在上面发文章。我知道上面高手多多,我只是一个小菜菜,希望高手牛人们多指点。。。
通过一个循环链表模拟猫抓老鼠的游戏过程,实现了一个简单的算法来找出最后一个被放生的老鼠的位置。
1540

被折叠的 条评论
为什么被折叠?



