题目:
A、B、C、D、E五名学生有可能参加计算机竞赛,根据下列条件判断哪些人参加了竞赛:
(1)A参加时,B也参加;
(2)B和C只有一个人参加;
(3)C和D或者都参加,或者都不参加;
(4)D和E中至少有一个人参加;
(5)如果E参加,那么A和D也都参加。
思路:
1. A = 1 then B = 1 ; A= 0 then B = 1 or 0
2. B = 1 then C = 0 ; B = 0 then C = 1;
3. C = D ( C + D = 1 or C + D = 0 )
4. D + E >= 1
5.E = 1 then A = B = 1
1 static void Main(string[] args) 2 { 3 //0 is false ,1 is true. 4 string[] name = { "A", "B", "C", "D", "E" }; 5 int[] i = new int[5]; 6 for (i[0] = 0; i[0] < 2; i[0]++) 7 { 8 for (i[1] = 0; i[1] < 2; i[1]++) 9 { 10 for (i[2] = 0; i[2] < 2; i[2]++) 11 { 12 for (i[3] = 0; i[3] < 2; i[3]++) 13 { 14 for (i[4] = 0; i[4] < 2; i[4]++) 15 { 16 if ((i[0] <= i[1]) && (i[1] + i[2] == 1) && (i[2] == i[3]) && (i[3] + i[4] >= 1) && 17 (i[4] == 0 || i[4] + i[0] + i[3] == 3)) 18 { 19 int index = 0; 20 foreach (var a in i) 21 { 22 if (a == 1) 23 { 24 Console.WriteLine(name[index] + " will join"); 25 } 26 27 else 28 { 29 Console.WriteLine(name[index] + " will not join"); 30 } 31 index++; 32 } 33 34 } 35 } 36 } 37 } 38 } 39 } 40 Console.ReadKey(); 41 }
运行结果:
时间: 2024-10-09 00:33:17