穷举法应用

请回答下面10个问题,各题都恰有一个答案是正确的:
(1)第一个答案是B的问题是哪一个?
A、2
B、3
C、4
D、5
E、6
(2)恰好有两个连续问题的答案是一样的,它们是:
A、2,3
B、3,4
C、4,5
D、5,6
E、6,7
(3)本问题答案和哪一个问题的答案相同?
A、1   
B、2   
C、4
D、7
E、6
(4)答案是A的问题的个数是:
A、0
B、1
C、2
D、3
E、4
(5)本问题答案和哪一个问题的答案相同?
A、10
B、9
C、8
D、7
E、6
(6)答案是A的问题的个数和答案是什么的问题的个数相同?
A、B
B、C
C、D
D、E
E、以上都不是
(7)按照字母顺序,本问题的答案和下一个问题的答案相差几个字母?
A、4
B、3
C、2
D、1
E、0(注:A和B相差一个字母)
(8)答案是元音字母的问题的个数是:
A、2
B、3
C、4
D、5
E、6(注:A和E是元音字母)
(9)答案是辅音字母的问题的个数是:
A、一个质数      7
B、一个阶乘数  6
C、一个平方数  4
D、一个立方数  8
E、5的倍数     5
(10)本问题的答案是:
A、A
B、B
C、C
D、D
E、E

5的10次方大约有900多万我的机器受不了
所以先加入一些简单的限制条件减少穷举的目标

显然第一题不能选B,第三题不能选A第9题不能选E(因为5是5的倍数也是质数),所以第8题也不能选D
(其实89两题等于一题,选了一个另一个就确定了),加入这4个条件以后,正好有400万种可能性,我的机器受得了了
如果机器再差一点,89联动可以减少到100万种可能性
然后对每种可能性检查答案,算出来可能性有3种,有人说5种,不知道怎么来的最好的答案是
0:C 1:D 2:E 3:B 4:E 5:E 6:D 7:C 8:B 9:A


using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace abcde
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ArrayList result = new ArrayList();
                char[] answer = new char[] { 'A', 'B', 'C', 'D', 'E' };
                AddResult(new char[10],0,answer, result);
                Console.WriteLine(result.Count);
                result=cond1(result);
                Console.WriteLine(result.Count );
                foreach (char[] chr in result)
                {
                    for (int i = 0; i < chr.Length; i++)
                    {
                        Console.Write("{0}:{1} ", i, chr[i]);
                    }
                    Console.WriteLine();
                }
          
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally {
                Console.WriteLine("End");
                Console.Read();
            }
        }

        static ArrayList cond1(ArrayList result) {
            ArrayList ans = new ArrayList();
            foreach (char[] chr in result)
            {
                bool validate = true;
                //开始检查           
                /*(1)第一个答案是B的问题是哪一个?
A、2
B、3
C、4
D、5
E、6*/
                int bIndex = Array.IndexOf(chr, 'B');
                int aCount = 0, bCount = 0, cCount = 0, dCount = 0, eCount = 0;
                foreach (char c in chr) {
                    if (c == 'A') { aCount++; }
                    else if (c == 'B') { bCount++; }
                    else if (c == 'C') { cCount++; }
                    else if (c == 'D') { dCount++; }
                    else if (c == 'E') { eCount++; }

                }
                if (
                    !((bIndex == 1 && chr[0] == 'A')
                    || (bIndex == 2 && chr[0] == 'B')
                    || (bIndex == 3 && chr[0] == 'C')
                    || (bIndex == 4 && chr[0] == 'D')
                    || (bIndex == 5 && chr[0] == 'E')
                    ))
                {
                    validate = false;
                }
                    /*(2)恰好有两个连续问题的答案是一样的,它们是:
A、2,3
B、3,4
C、4,5
D、5,6
E、6,7*/
                else if (
                    !((chr[1]=='A' && chr[1]==chr[2])
                    || (chr[1] == 'B' && chr[2] == chr[3])
                    || (chr[1] == 'C' && chr[3] == chr[4])
                    || (chr[1] == 'D' && chr[4] == chr[5])
                    || (chr[1] == 'E' && chr[5] == chr[6])
                    )){
                        validate = false;
                }
                /*(3)本问题答案和哪一个问题的答案相同?
A、1    --不可能
B、2   
C、4
D、7
E、6
                 */
                else if(!(
                    (chr[2]=='A' && chr[0]=='A')
                    || (chr[2] == 'B' && chr[1] == 'B')
               || (chr[2] == 'C' && chr[3] == 'C')
               || (chr[2] == 'D' && chr[6] == 'D')
               || (chr[2] == 'E' && chr[5] == 'E')
                    )){
                    validate=false;
                }
                /*(4)答案是A的问题的个数是:
A、0
B、1
C、2
D、3
E、4
                 */
                else if (!(
                   (chr[3] == 'A' && aCount == 0)
               || (chr[3] == 'B' && aCount == 1)
               || (chr[3] == 'C' && aCount == 2)
               || (chr[3] == 'D' && aCount == 3)
               || (chr[3] == 'E' && aCount == 4)
               )) {
                   validate = false;
                }
                /*(5)本问题答案和哪一个问题的答案相同?
A、10
B、9
C、8
D、7
E、6*/
                else if(!(
                    (chr[4]=='A' && chr[9]=='A')
               || (chr[4] == 'B' && chr[8] == 'B')
               || (chr[4] == 'C' && chr[7] == 'C')
               || (chr[4] == 'D' && chr[6] == 'D')
               || (chr[4] == 'E' && chr[5] == 'E')
                    )){
                        validate = false;
                }
                    /*(6)答案是A的问题的个数和答案是什么的问题的个数相同?
A、B
B、C
C、D
D、E
E、以上都不是*/
                else if(!(
                    (chr[5]=='A' && aCount==bCount)
                    || (chr[5]=='B' && aCount==cCount)
               || (chr[5] == 'C' && aCount == dCount)
               || (chr[5] == 'D' && aCount == eCount)
                   ||(chr[5]=='E' && (aCount!=bCount && aCount!=cCount && aCount!=dCount && aCount!=eCount))
                    )){
                        validate = false;
                }
                /*(7)按照字母顺序,本问题的答案和下一个问题的答案相差几个字母?
A、4
B、3
C、2
D、1
E、0(注:A和B相差一个字母)*/
                else if(!(
                    (chr[6]=='A' && Math.Abs(chr[6] - chr[7])==4)
               || (chr[6] == 'B' && Math.Abs(chr[6] - chr[7]) == 3)
               || (chr[6] == 'C' && Math.Abs(chr[6] - chr[7]) == 2)
               || (chr[6] == 'D' && Math.Abs(chr[6] - chr[7]) == 1)
               || (chr[6] == 'E' && Math.Abs(chr[6] - chr[7]) == 0)
                    )){
                        validate = false;
                }
                /*(8)答案是元音字母的问题的个数是:
A、2
B、3
C、4
--D、5
E、6(注:A和E是元音字母)
                 */
                else if(!(
                    (chr[7]=='A' && aCount+eCount==2)
               || (chr[7] == 'B' && aCount + eCount == 3)
               || (chr[7] == 'C' && aCount + eCount == 4)
               || (chr[7] == 'D' && aCount + eCount == 5)
               || (chr[7] == 'E' && aCount + eCount == 6)
                    )){
                        validate = false;
                }
                /*(9)答案是辅音字母的问题的个数是:
A、一个质数    7
B、一个阶乘数  6
C、一个平方数  4
D、一个立方数  8
--E、5的倍数     5*/
                else if (!(
               (chr[8] == 'A' && bCount + cCount + dCount == 7)
          || (chr[8] == 'B' && bCount + cCount + dCount == 6)
          || (chr[8] == 'C' && bCount + cCount + dCount == 4)
          || (chr[8] == 'D' && bCount + cCount + dCount == 8)
          || (chr[8] == 'E'&& bCount + cCount + dCount == 5)
               ))
                {
                    validate = false;
                }
                if (validate)
                {
                    ans.Add(chr);
                }
            }
            return ans;
          
        }
        static void AddResult(char[] de,int index,char[] an,ArrayList res){
           
            foreach (char chr in an) {
                if (index == 0 && chr == 'B') {
                    continue;
                }
                else if (index == 3 && chr == 'A') {
                    continue;
                }
             
                else if (index == 7 && chr == 'D')
                {
                    continue;
                }
                else if (index == 8 && chr == 'E')
                {
                    continue;
                }

                char[] newde=new char[10];
                Array.Copy(de,newde,index);
                newde[index] = chr;
               
                if (index < de.Length - 1)
                {
                    AddResult(newde, index + 1, an, res);
                }
                else {
                    res.Add(newde);
                }
            }
           
           
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值