有N只猴子选大王,选举的办法是:排成一排,从头到尾报数,报到3的倍数(3、6、9、……)的退出去,直到全部报完,然后从尾到头开始逆向报数,同样报3的倍数的退出.第三遍又是从头到尾进行,
来源:学生作业帮助网 编辑:作业帮 时间:2024/05/13 09:01:33
有N只猴子选大王,选举的办法是:排成一排,从头到尾报数,报到3的倍数(3、6、9、……)的退出去,直到全部报完,然后从尾到头开始逆向报数,同样报3的倍数的退出.第三遍又是从头到尾进行,
有N只猴子选大王,选举的办法是:排成一排,从头到尾报数,报到3的倍数(3、6、9、……)的退出去,直到全部报完,然后从尾到头开始逆向报数,同样报3的倍数的退出.第三遍又是从头到尾进行,第四遍又从尾到头,直到最后余下两只,以排在后面(按报数的顺序)的那只为猴王.请编程序找出猴王的位置.(N从键盘输入)(用PASCAL)
有N只猴子选大王,选举的办法是:排成一排,从头到尾报数,报到3的倍数(3、6、9、……)的退出去,直到全部报完,然后从尾到头开始逆向报数,同样报3的倍数的退出.第三遍又是从头到尾进行,
这个是C语言编写,
题目是输入两个正整数 n 和 m( (1data=x;
r=p->next;
q->next=r;
p->next=q;
}
void delect(Position p){// delect the element after p
PtrToNode tmp;
tmp=p->next;
p->next=tmp->next;
free(tmp);
}
void MakeList(List *L,int n){//make the list
int i;
Position p=L;
for(i=1;inext;
}
}
void main()
{
int i,j,n,m;
Position p,q,r;
List L;
double duration;
while(scanf("%d%d",&n,&m)!=EOF){//input the number to slect
if(n>MAX){
printf("The number is too large!\n");
continue;
}
init(&L);//initilze the list
MakeList(L,n);//make the list
start = clock();
q=p=L->next;// p and q are the first position of the list
for(i=0;inext; // r is the position of the element we select
if(r==NULL)r=q;
if(i!=n-1)printf("No%d:%d\n",i+1,r->data);//print the element
else printf("Last No is:%d\n",r->data);
if(p->next==NULL){
delect(L);
q=L->next;//delect the position of p->next
}
else delect(p);
p=p->next;
if(p==NULL)p=q;
}
stop = clock();
duration = ((double)(stop - start))/CLK_TCK;
printf("The time is %f\n",duration);
}
}
什么意思嘛,说清楚一点,我也觉得会打起来。不现实
猴子打起来
只会打起来