早教吧作业答案频道 -->其他-->
有N只猴子选大王,选举的办法是:排成一排,从头到尾报数,报到3的倍数(3、6、9、……)的退出去,直到全部报完,然后从尾到头开始逆向报数,同样报3的倍数的退出.第三遍又是从头到尾进行,
题目详情
有N只猴子选大王,选举的办法是:排成一排,从头到尾报数,报到3的倍数(3、6、9、……)的退出去,直到全部报完,然后从尾到头开始逆向报数,同样报3的倍数的退出.第三遍又是从头到尾进行,第四遍又从尾到头,直到最后余下两只,以排在后面(按报数的顺序)的那只为猴王.请编程序找出猴王的位置.(N从键盘输入)(用PASCAL)
▼优质解答
答案和解析
这个是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);
}
}
题目是输入两个正整数 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);
}
}
看了 有N只猴子选大王,选举的办法...的网友还看了以下:
有N只猴子选大王,选举的办法是:排成一排,从头到尾报数,报到3的倍数(3、6、9、……)的退出去, 2020-05-16 …
猴子选大王pascaln只猴子选大王,选举办法如下:从头到尾1,2,3报数,凡报3的退出,余下的从 2020-05-17 …
pascal N只猴子选大王 恩 程序尽量简单.初学者水平N只猴子选大王.选举办法如下:从头到尾1 2020-05-17 …
(1)有2013名学生排成一行,从头起编成1,2,…,2013号,第一次从头到尾“1,2,3”报数 2020-07-09 …
2009个同学,从排头到排位1~3报数,又从排尾到排头1~5报数.两次报数和不小于7的有()人200 2020-11-17 …
请教一道数学题感激不尽100名同学按照编号1---100从左往右顺次排成1行,然后1.2报数到最后一 2020-11-17 …
一100名同学按照编号1至100从左往右顺次排成一行,然后“1、2”报数到最后一人,凡报“1”的就出 2020-11-24 …
谁能教我一道数学题100名同学按照编号1~100从左往右顺次排成一行,然后“1、2”报数到最后一人, 2020-11-24 …
一道数学思考题100名同学按照1至100从左往右顺次排成一行,然后“1、2”报数到最后一人,凡报“1 2020-11-24 …
100名同学按照编号1——100从左往右顺次排成一行,然后“1、2”报数到最后一人,凡报“1”的同学 2020-12-31 …