返回信息流海浪 15:43:50
出道题目给大家做做:有点难度。
TOTAL个人围成一圈,每次数到N的人退出,求最后剩下的一个人的编号。填空…
Final int TOTAL=15;
int xxxx(int N)
{
int ring[TOTAL] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int nextstart = 0,counter=0;
for (int i = 1; i<TOTAL; i++)
{
counter = 0;
while(counter < N)
{
if (______________;)
______________;
else
______________;
}
ring[nextstart] = 0;
}
return nextstart +1;
}
这是一条镜像帖。来源:北邮人论坛 / soft-design / #2098同步于 2005/12/2
SoftDesign机器人发帖
老题,大家来研究研究
Neverwinter
2005/12/2镜像同步5 回复
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
My answer(这个方法只适用于这个情况:15人中有14人退出,这个方法求的是最后退出人的编号,而不是剩下来的人的编号):
if (ring[nextstart])
counter++, nextstart+=(counter==0)?1:0, nextstart+=(counter==N)?0:1;
else
nextstart=(nextstart>=TOTAL-1)?0:(nextstart+1);
my answser, based on NE's code,
added some printfs for test,
tested under VC.net2k3(since i am not familiar with java)
still, i have a question on this problem, the man start counting.
here is a result with N = 9
Result for N = 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1th round (nextstart 9 ): 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
2th round (nextstart 4 ): 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
3th round (nextstart 0 ): 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
4th round (nextstart 12 ): 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1,
5th round (nextstart 10 ): 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1,
6th round (nextstart 8 ): 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1,
7th round (nextstart 11 ): 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1,
8th round (nextstart 13 ): 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1,
9th round (nextstart 1 ): 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1,
10th round (nextstart 5 ): 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1,
11th round (nextstart 2 ): 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1,
12th round (nextstart 3 ): 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1,
13th round (nextstart 14 ): 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0,
14th round (nextstart 6 ): 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
u could see that although the first 0 is labled 9 respected to nextstart ( 9 here ), but we counted 10 times in fact.
// Exp01.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
const int TOTAL=15;
int T(int N)
{
int ring[TOTAL] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int nextstart = 0,counter=0;
// for the title
printf(" Result for N = %-10d",N);
for(int j = 0;j<TOTAL;j++)
{
printf("%2d ",j);
}
putchar('\n');
// insert code end
for (int i = 1; i<TOTAL; i++)
{
counter = 0;
while(counter < N)
{
if ( ( counter == N - 1 ) || ring[nextstart] != 0 )
( nextstart = (nextstart + 1) % TOTAL ) , counter += ( counter == N - 1 ) ? ( ( ring[nextstart] == 1 ) ? 1:0 ) : 1;
else
nextstart = (nextstart + 1) % TOTAL;
}
// since nextstart is the pointer to the one who was unlucky,
// so we must make sure that ring[nextstart] always be 1 when we come here
// so how we make sure?
// when counter == N - 1 we should have ring[(nextstart + 1) % TOTAL] == 1
// and if we hold N - 1 in counter all the time then we got an unlimited loop!
ring[nextstart] = 0;
// output result for each round
printf("%2dth round (nextstart %2d ): ",i,nextstart);
for(int j = 0;j<TOTAL;j++)
{
printf(" %d,",ring[j]);
}
putchar('\n');
// end
}
return nextstart +1;
}
int main(void)
{
for(int i = 1;i< 30;i++)
{
T(i);
putchar('\n');
}
return 0;
}
my answer is not so complicated.
here is my answer(after fixed):
if (ring[nextstart] == 0)
nextstart = (nextstart + 1) % TOTAL;
else
[color=red]++counter, nextstart = (counter == N) ? nextstart : (nextstart + 1) % TOTAL;[/color]
be careful the red line. i don't know whether it's compiler-dependent.
【 在 alexxin 的大作中提到: 】
: my answser, based on NE's code,
: added some printfs for test,
: tested under VC.net2k3(since i am not familiar with java)
: ...................
int xxxx(int N)
{
int ring[TOTAL] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
// int ring[TOTAL] = {1,1,1,1,1};
int nextstart = 0,counter=0;
for (int i = 1; i<TOTAL; i++)
{
counter = 0;
//printf("%d is at beginning \n",nextstart);
while(counter < N)
{
if (ring[nextstart%TOTAL]== 1)
{
nextstart=nextstart%TOTAL+1;
counter++;
if(counter == N)
{
nextstart--;
}
}
else
{
nextstart=nextstart%TOTAL+1;
}
}
ring[nextstart] = 0;
printf("!!!!!!!!!!!!!!!! [%d] is out\n",nextstart+1);
}
return nextstart +1;
}
TOTAL=15 N =5输出:
!!!!!!!!!!!!!!!! [5] is out
!!!!!!!!!!!!!!!! [10] is out
!!!!!!!!!!!!!!!! [15] is out
!!!!!!!!!!!!!!!! [6] is out
!!!!!!!!!!!!!!!! [12] is out
!!!!!!!!!!!!!!!! [3] is out
!!!!!!!!!!!!!!!! [11] is out
!!!!!!!!!!!!!!!! [4] is out
!!!!!!!!!!!!!!!! [14] is out
!!!!!!!!!!!!!!!! [9] is out
!!!!!!!!!!!!!!!! [8] is out
!!!!!!!!!!!!!!!! [13] is out
!!!!!!!!!!!!!!!! [2] is out
!!!!!!!!!!!!!!!! [7] is out
如果最后一个填空非要用一条语句完成的,可以写成
nextstart = ((++count)==N) ? nextstart : (nextstart + 1) % TOTAL;