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(); }
添加回答
舉報