static
void
light(Boolean[] lights,
int
n)
{
if
(n <= 1 || lights.Length<5)
return
;
for
(
int
i = 0; i < lights.Length; i++)
{
if
((i + 1) % n == 0) lights[i] = !lights[i];
}
light(lights, n - 1);
}
static
void
Main(
string
[] args)
{
Boolean[ ] list=
new
Boolean[10];
//共10盏灯,自行修改
for
(
int
i = 0; i < list.Length; i++) list[i] =
true
;
light(list, 5);
for
(
int
i = 0; i < list.Length; i++)
if
(list[i]) Console.WriteLine(
"第"
+ (i + 1) +
"个灯亮"
);
Console.ReadLine();
}
public class Lamp { public bool State = true ; public int Num; public void Toggle() { State = !State; } } |
1
2
3
4
5
6
7
8
9
10
11
|
int N = 10; List<Lamp> lamps = new List<Lamp>(); for ( int i = 1; i <= N; i++) { lamps.Add( new Lamp() { Num = i }); } lamps.Where(p => p.Num % 2 == 0).ToList().ForEach(p => p.Toggle()); lamps.Where(p => p.Num % 3 == 0).ToList().ForEach(p => p.Toggle()); lamps.Where(p => p.Num % 4 == 0).ToList().ForEach(p => p.Toggle()); lamps.Where(p => p.Num % 5 == 0).ToList().ForEach(p => p.Toggle()); lamps.Where(p => p.State == true ).ToList().ForEach(p => Console.WriteLine( " " + p.Num)); |