2 回答
TA贡献1812条经验 获得超5个赞
public static void DengOnOFf(int num)
{
//熄灭2的倍数
List<int> result = new List<int>();
List<int> result1 = new List<int>();
for (int i = 1; i < num + 1; i++)
{
if (i % 2 != 0)
{
result.Add(i);//亮着的灯
}
else
{
result1.Add(i);//灭的灯
}
}
//3的倍数
List<int> result2 = new List<int>();
List<int> result3 = new List<int>();
for (int i = 1; i < result.Count; i++)
{
if (i % 3 != 0)
{
result2.Add(i);//亮着的灯
}
else
{
result3.Add(i);//灭着的灯
}
}
for (int i = 1; i < result1.Count; i++)
{
if (i % 3 == 0)
{
result2.Add(i);//亮着的灯
}
else
{
result3.Add(i);//灭着的灯
}
}
//4的倍数
List<int> result4 = new List<int>();
List<int> result5 = new List<int>();
for (int i = 1; i < result2.Count; i++)
{
if (i % 4 != 0)
{
result4.Add(i);//亮着的灯
}
else
{
result5.Add(i);//灭着的灯
}
}
for (int i = 1; i < result3.Count; i++)
{
if (i % 4 == 0)
{
result4.Add(i);//亮着的灯
}
else
{
result5.Add(i);//灭着的灯
}
}
//5的倍数
//4的倍数
List<int> result6 = new List<int>();
List<int> result7 = new List<int>();
for (int i = 1; i < result4.Count; i++)
{
if (i % 5 != 0)
{
result6.Add(i);//亮着的灯
}
else
{
result7.Add(i);//灭着的灯
}
}
for (int i = 1; i < result5.Count; i++)
{
if (i % 5 == 0)
{
result6.Add(i);//亮着的灯
}
else
{
result7.Add(i);//灭着的灯
}
}
for (int i = 0; i < result6.Count; i++)
{
Console.Write(result6[i].ToString()+" ");
}
Console.Read();
}
看看是不是你想要的结果!
TA贡献1834条经验 获得超8个赞
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(); }
添加回答
举报