有只猫抓了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);
}
第一次在上面发文章。我知道上面高手多多,我只是一个小菜菜,希望高手牛人们多指点。。。