一道算法题

给出几种产品组合,求出产品最大优惠值

protected void Button1_Click(object sender, EventArgs e)
    {
        string str_input = TextBox1.Text;

        List<Product> list_Rule = new List<Product>();
        //规则
        list_Rule.Add(new Product("A", 1));
        list_Rule.Add(new Product("B", 1));
        list_Rule.Add(new Product("C", 2));
        list_Rule.Add(new Product("E", 1));
        list_Rule.Add(new Product("AB", 3));
        list_Rule.Add(new Product("BC", 4));
        list_Rule.Add(new Product("BDF", 6));
        list_Rule.Add(new Product("ABDF", 9));
        list_Rule.Add(new Product("ABCD", 10));
      
        var s1 = from v in list_Rule
                 select v.StrName;

        List<string> resultList = GetResult(str_input, s1.ToArray());
        string resultString = null;
        for (int i = 0; i < resultList.Count; i++)
        {
            resultString += resultList[i] + ",";
        }
        
        if (resultString.Length > 0) 
        {
            var s2 = from v in list_Rule
                     join o in resultList
                     on  v.StrName equals o.ToString()
                     select v.Price;            
            Label1.Text = "优惠"+ s2.Max() +"元";
        }
        else
            Label1.Text = "优惠O元";


    }

    private List<string> GetResult(string s2, string[] s1)
    {
        List<string> resultList = new List<string>();
        //先初步过滤长度比s2大的字符串
        List<string> newS1 = new List<string>();
        for (int i = 0; i < s1.Length; i++)
        {
            if (s1[i].Length <= s2.Length)
            {
                newS1.Add(s1[i]);
            }
        }
        int num2 = GetNum(s2);
        int itemNum = 0;
        for (int i = 0; i < newS1.Count; i++)
        {
            itemNum = GetNum(s1[i]);
            if ((itemNum & num2) == itemNum)
            {
                resultList.Add(s1[i]);
            }
        }
        return resultList;
    }

    //把字符串成位串一共26位,从低位到高位依次标识字符(a-z)是否在字符串中
    private int GetNum(string s)   
    {
        int n = 0;//4个字节=32位
        char aChar;
        char constChar = 'A';        
        int position = 0; //字符和'a'的差为k,根据映射规则,说明2进制数的
        //从低位算起的第(k+1)位上为1,对应的整数为2的k次方;
        //首先把每个字符串映射成26位的二进制数 
        //  zy xwvu tsrq ponm lkji hgfe dcba           
        //例如 s1[i]="abc"——>00 0000 0000 0000 0000 0000 0111=square(2,a-(a-1))+square(2,b-(a-1))++square(2,b-(a-1));
        //s2="cbeh"——>       00 0000 0000 0000 0000 1001 0110
        for (int i = 0; i < s.Length; i++)
        {
            aChar = s[i];
            position = (aChar - constChar);
            n += (int)Math.Pow(2, position);
        }
        return n;
    }
 



    class Product
    {
        String strName;
        int price;

        public Product(string p, int p_2)
        {
            // TODO: Complete member initialization
            this.strName = p;
            this.price = p_2;
        }


        public String StrName
        {
            get { return strName; }
            set { strName = value; }
        }


        public int Price
        {
            get { return price; }
            set { price = value; }
        }


    }

  虽然表面是解决了问题,但是细想想还是没有完美的解决这题,如果商品名称换成ID【数字】这个方法不行了。

      欢迎集思广益寻找出这题最终答案

posted on 2013-05-04 21:18  Karson007  阅读(114)  评论(0编辑  收藏  举报