主题:高手请进(约瑟夫问题2)
xx小小迷
[专家分:80] 发布于 2008-06-22 17:43:00
将1~M这M个数按从小到大的顺序沿顺时针方向围成一圈,以S为起点。先沿顺时针方向数到第N个数出圈,再沿逆时针方向数到第K个数出圈,再沿顺时针方向数到第N个数出圈,再沿逆时针方向数到第K个数出圈......直到全部出圈为止。
请输出依次出圈的序列。
【样例输入】依次输入M,S,N,K,每行仅输入一个。
8
1
3
2
【样例输出】统一行输出,数字与数字之间加空格。
3 1 5 2 7 4 6 8
回复列表 (共1个回复)
沙发
flyinggoon [专家分:110] 发布于 2008-07-01 10:56:00
CLS : INPUT m, s, n, k: DIM sm(m)
FOR i = 1 TO m: sm(i) = 0: NEXT i
i = 0: pointm = s - 1
DO
IF i MOD 2 = 0 THEN
stepm = 1: endm = n
ELSE
stepm = -1: endm = k
END IF
summ = 0
DO
pointm = pointm + stepm
IF stepm = 1 THEN
pointm = (pointm - 1) MOD m + 1
ELSE
IF pointm < 1 THEN pointm = m
END IF
IF sm(pointm) = 0 THEN summ = summ + 1
LOOP UNTIL summ = endm
sm(pointm) = 1: PRINT pointm; : i = i + 1
LOOP UNTIL i = m
END
我来回复