A、B、C、D、E五名学生有有可能参加计算机竞赛,根据下列条件判断哪些人参加了竞赛:
(1)A参加时,B也参加;
(2)B和C只有一个人参加;
(3)c和d或者都参加,或者都不参加;
(4)d和e至少有一个人参加;
(5)如果e参加,那么a和d也都参加;
难点在
bool a = Convert.ToBoolean((exp & 16) >> 4);
bool b = Convert.ToBoolean((exp & 8) >> 3);
bool c = Convert.ToBoolean((exp & 4) >> 2);
bool d = Convert.ToBoolean((exp & 2) >> 1);
bool e = Convert.ToBoolean(exp & 1);
简单解释:a,b,c,d,e参加了为true,反之false 用0 1表示; 所以总共就有32种情况 : a b c d e
0 0 0 0 0
0 0 0 0 1
0 0 0 1 0
.
.
.
1 1 1 1 1
这样的32种情况。
得到a的二进制值:(exp & 16) >> 4); 这个还不晓得怎么解释,自己先想,联合上面的表想!!不难想到!
(1)A参加时,B也参加;
(2)B和C只有一个人参加;
(3)c和d或者都参加,或者都不参加;
(4)d和e至少有一个人参加;
(5)如果e参加,那么a和d也都参加;
class Program
{
static void Main(string[] args)
{
Dowork();
}
public static void Dowork()
{
for (int i = 0; i < 32; i++)
{
if (Logic(i))
{
Console.Write("A:{0},B:{1},C:{2},D:{3},E:{4}", ((i & 16) >> 4) == 1 ? true : false, ((i & 8) >> 3) == 1 ? true : false, ((i & 4) >> 2) == 1 ? true : false, ((i & 2) >> 1) == 1 ? true : false, ((i & 1)) == 1 ? true : false);
Console.ReadKey();
}
}
}
public static bool Logic(int exp)
{
bool a = Convert.ToBoolean((exp & 16) >> 4);
bool b = Convert.ToBoolean((exp & 8) >> 3);
bool c = Convert.ToBoolean((exp & 4) >> 2);
bool d = Convert.ToBoolean((exp & 2) >> 1);
bool e = Convert.ToBoolean(exp & 1);
if (a == b && b != c && c == d && !(d == false && e == false))
{
if (e == true)
{
if (a == true && d == true)
{
return true;
}
else
return false;
}
else
return true;
}
return false;
}
}
难点在
bool a = Convert.ToBoolean((exp & 16) >> 4);
bool b = Convert.ToBoolean((exp & 8) >> 3);
bool c = Convert.ToBoolean((exp & 4) >> 2);
bool d = Convert.ToBoolean((exp & 2) >> 1);
bool e = Convert.ToBoolean(exp & 1);
简单解释:a,b,c,d,e参加了为true,反之false 用0 1表示; 所以总共就有32种情况 : a b c d e
0 0 0 0 0
0 0 0 0 1
0 0 0 1 0
.
.
.
1 1 1 1 1
这样的32种情况。
得到a的二进制值:(exp & 16) >> 4); 这个还不晓得怎么解释,自己先想,联合上面的表想!!不难想到!
本文深入探讨了五名学生参加计算机竞赛的逻辑条件,并通过编程实现展示了如何运用位运算解决组合问题。详细解释了如何根据给定条件判断学生是否参加竞赛,以及算法背后的逻辑思考过程。
975

被折叠的 条评论
为什么被折叠?



