|
发表于 2008-11-14 13:12:38
|
显示全部楼层
以下是代码:6 v: H# M& Z$ V* z: n& ~
public class PeopleAndLight {
& w) h6 o* S: w* a; f% \0 u# }5 h/ V
public static void main(String[] args) {
( ]* k' i: v4 ]0 z int[] People = new int[100];
8 [+ e' q; g8 {9 ~ boolean[] Way = new boolean[100];
. D. V9 B* }8 `7 `& E9 X [8 D, ?% E3 H6 Z' ?/ F
for (int i = 0; i < 100; i++) { |2 G% W, h4 h( n n; q/ {
People = i + 1; // 设置人的编号2 |, `" W& j/ M3 C
Way = false;
) y# x+ H4 T* M" k9 l) E+ Y }
2 K9 V% Q2 g4 M& N o( r5 a5 ~0 ?# ]( |' ?& {' `" d
for (int i=0; i<;People.length; i++) {) B9 f# U4 N5 N% n, M
int pno = i + 1; // 当前某人的编号2 B# U- _' L' R3 G% w
for (int j=0; j<Way.length; j++) {
P- x: Z$ Y3 o int lno = j + 1; //当前灯的编号
( G* y1 V8 m4 Q9 ?$ @/ x+ c- W if (lno % pno == 0)
9 t2 j' m6 c8 ]9 X Way[j] = !Way[j];9 e/ @6 C) u3 I9 \$ z) I" C
}
; Z$ U! C3 @+ R8 S6 { }
1 U- l/ r7 T4 _* T5 a3 \2 |. c, C
3 F$ p* _' T9 j$ Z* p int count = 0;- L% v: I5 P, z; z% _
for (int i=0; i<Way.length; i++) {; e! C+ m$ ]+ m5 e% Z
if (Way == true)- _+ k1 h8 \# i5 l/ ^0 _
count ++;
) G1 ~4 |; [; V8 b5 g8 k }' e/ B, l2 W- g. l" X5 C% a7 T
, w4 c: C' |/ h, X System.out.println(count);8 z1 g$ c6 ?2 [: O8 @, h5 C
}: y' Z0 i) }$ l6 B4 h3 i$ N" {
}+ L* g2 _8 C$ f4 y/ L7 N, m
# `2 j% d9 U k" Y6 {5 S# i9 K- s
编程环境:JAVA 1.6 IDE:ECLIPSE 3.2 OS:UBUNTU LINUX 8.108 r2 [" H- i0 j; o1 R
& X" x. W$ N8 f& B! B2 P7 i这题很容易,不被迷惑就很容易。& m* m* p1 q+ F+ v. f0 g) C( P% v
+ { D; W+ @- x+ v7 Q一个人一个人的过去,和一群人按顺序过去的结果是一样的4 X% s/ E X$ x/ @* P( x
8 O# w$ E. F" P# W5 B. ~ `0 U" c! W
结果是:10
# O- E+ Z$ L4 V# b; t- s8 p9 n3 X( J L' i" E b. D4 h
0 d) k, P% w5 ?
; ^- E8 `/ c* A0 J4 I1 s0 B7 R+ e: B, H
[ 本帖最后由 charon 于 2008-11-16 23:02 编辑 ] |
|