`
cfan1874
  • 浏览: 42212 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

A、B、C、D、E五名学生有可能参加计算机竞赛,根据条件判断哪些人参加了竞赛

 
阅读更多
A、B、C、D、E五名学生有有可能参加计算机竞赛,根据下列条件判断哪些人参加了竞赛:
(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 & >> 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);    这个还不晓得怎么解释,自己先想,联合上面的表想!!不难想到!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics