现我有规则如下:
如果买商品A+B任意组合数量等于2,则赠D一个
(可能1*A+1*B=2,2*A+0*B=2,0*A+2*B=2)
如果买商品B   数量等于2,       则赠E一个
如果买商品C   数量等于2,       则赠F一个现我买A商品2个,B商品2个,C商品2个
如何求得我可能得到的赠品及数量?
各位高手请给出解?谢谢

解决方案 »

  1.   

    没说明白
    1*A+2*B=3是不是得D和E?
      

  2.   

    组合一次后,A和B都要减去相应进行组合的数量吧.真拗口.
    就是说有10个A10个B,1A+1B后,在组合就是9个A和9个B了吧
      

  3.   

    A和B两个任意组合或任何一个数量=2,则可以赠D一个
    并且B的数量=2时,可以赠E 一个但是,如果你选择了赠品D,你要减去D所占的份额数量,
    在看B的剩余数量还可否够,选择赠品E如果1*A+2*B=3 
       你选择了D,B的剩余数量是1,你就没有选择E的权利
      

  4.   

    static void Main(string[] args)
            {
                Program p = new Program();
                int[] def = p.ok(2, 3, 5);
                foreach (int i in def)
                {
                    Console.WriteLine(i);
                }
                Console.ReadKey();
            }        private int[] ok(int a, int b, int c)
            {
                int[] def = new int[3];
                def[0] = (a + b) / 2;
                def[1] = b / 2;
                def[2] = c / 2;
                return def;
            }
      

  5.   

    static void Main(string[] args)
            {
                Program p = new Program();
                int[] def = p.ok(2, 3, 5);
                foreach (int i in def)
                {
                    Console.WriteLine(i);
                }
                Console.ReadKey();
            }        private int[] ok(int a, int b, int c)
            {
                int[] def = new int[3];
                def[0] = (a + b) / 2;
                def[1] = b / 2;
                def[2] = c / 2;
                return def;
            }是等于或大于是吧。。如果只是等于的话。那么就太简单了直接减个2看是不是等于0
      

  6.   

    有以下可能:
    1、3个D
    2、2个D和1个E
    3、2个D和1个F
    4、1个D和1个E和1个F
      

  7.   

    class Program
        {
            static void Main(string[] args)
            {
                Program p = new Program();
                int a=2;
                int b=3;
                int c=4;
                int[] def = p.ok(a, b, c);
                foreach (int i in def)
                {
                    Console.WriteLine(i);
                }
                Console.WriteLine("请选择D赠品数量");  //在这里没有作任何的越界判断和异常处理,因为我在做事,没太多时间。。
                int d = int.Parse(Console.ReadLine());
                if (d - def[0] == 0)
                {
                    def[1] = 0;
                }
                else
                {
                    d -= a / 2;
                    if (d > 0)
                    {
                        def[1] -= d;
                    }
                }
                foreach (int i in def)
                {
                    Console.WriteLine(i);
                }
                Console.ReadKey();
            }        private int[] ok(int a, int b, int c)
            {
                int[] def = new int[3];
                def[0] = (a + b) / 2;
                def[1] = b / 2;
                def[2] = c / 2;
                return def;
            }
        }晕。没看清题。改了改。。
      

  8.   

    to:jhkemail
      
      你的算法中在哪能体现出以下规则:
      如果你选择了赠品D,你要减去D所占的份额数量,
      在看B的剩余数量还可否够,选择赠品E
      

  9.   

    class Program
        {
            static void Main(string[] args)
            {
                Program p = new Program();
                int a=2;
                int b=3;
                int c=4;
                int[] def = p.ok(a, b, c);
                foreach (int i in def)
                {
                    Console.WriteLine(i);    //你可以选择的赠品 DEF
                }
                Console.WriteLine("请选择D赠品数量");  //在这里没有作任何的越界判断和异常处理,因为我在做事,没太多时间。。
                int d = int.Parse(Console.ReadLine());   //在这里选择你所要的D赠品数量
                if (d - def[0] == 0)
                {
                    def[1] = 0;
                }
                else
                {
                    d -= a / 2;
                    if (d > 0)
                    {
                        def[1] -= d;
                    }
                }
                foreach (int i in def)
                {
                    Console.WriteLine(i);  //你最后可以得到的赠品 DEF
                }
                Console.ReadKey();
            }        private int[] ok(int a, int b, int c)   //得到可选赠品数量
            {
                int[] def = new int[3];
                def[0] = (a + b) / 2;
                def[1] = b / 2;
                def[2] = c / 2;
                return def;
            }
        }改好了。你看看是不是这样。。干活去了。。
      

  10.   

    class Program
        {
            static void Main(string[] args)
            {
                Program p = new Program();
                int a=2;
                int b=3;
                int c=4;
                int[] def = p.ok(a, b, c);
                foreach (int i in def)
                {
                    Console.WriteLine(i);    //你可以选择的赠品 DEF
                }
                Console.WriteLine("请选择D赠品数量");  //在这里没有作任何的越界判断和异常处理,因为我在做事,没太多时间。。
                int d = int.Parse(Console.ReadLine());   //在这里选择你所要的D赠品数量
                if (d - def[0] == 0)
                {
                    def[1] = 0;
                }
                else
                {
                    d -= a / 2;
                    if (d > 0)
                    {
                        def[1] -= d;
                    }
                }
                foreach (int i in def)
                {
                    Console.WriteLine(i);  //你最后可以得到的赠品 DEF
                }
                Console.ReadKey();
            }        private int[] ok(int a, int b, int c)   //得到可选赠品数量
            {
                int[] def = new int[3];
                def[0] = (a + b) / 2;
                def[1] = b / 2;
                def[2] = c / 2;
                return def;
            }
        }改好了,你看看是不是这样。干活去了。。
      

  11.   

    没看懂:      private int[] ok(int a, int b, int c)   //得到可选赠品数量
            {
                int[] def = new int[3];
                def[0] = (a + b) / 2;
                def[1] = b / 2;
                def[2] = c / 2;
                return def;
            }
    你这里是返回的赠品数量呢,还是可得的赠品种类?
      

  12.   

    public enum FavorFirstChoice
        {
            A,
            B,
            Unknown
        }    public enum Largess
        {
            D,
            E,
            F
        }
            private Dictionary<string, int> _goods;        _goods = new Dictionary<string, int>();
            _goods.Add("A", 2);
            _goods.Add("B", 2);
            _goods.Add("C", 2);        Dictionary<string, int> test = LargessArithmetic(new string[] { "D", "E", "F" }, FavorFirstChoice.A);        private Dictionary<string, int> LargessArithmetic(string[] largessSerials, FavorFirstChoice favor)
            {
                Dictionary<string, int> ret = new Dictionary<string, int>();
                foreach (string largess in largessSerials)
                {
                    MethodInfo vMethodInfo = this.GetType().GetMethod("LargessCalc", BindingFlags.Instance | BindingFlags.NonPublic);
                    int tmp = 0;
                    if (vMethodInfo != null)
                    {
                        tmp = (int)vMethodInfo.Invoke(this, new object[] { favor, (Largess)Enum.Parse(typeof(Largess), largess) });
                    }
                    ret[largess] = tmp;
                }            return ret;
            }        private int LargessCalc(FavorFirstChoice favor, Largess largess)
            {
                string methodName = largess.ToString() + "Largess";
                MethodInfo vMethodInfo = this.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
                if (vMethodInfo != null)
                {
                    return (int)vMethodInfo.Invoke(this, new object[] { favor });
                }
                else return 0;
            }        private int DLargess(FavorFirstChoice favor)
            {
                int ret = 0;
                int tmpA = 0;
                int tmpB = 0;
                if (_goods.ContainsKey("A")) tmpA = _goods["A"];
                if (_goods.ContainsKey("B")) tmpB = _goods["B"];            if (favor == FavorFirstChoice.A)
                {
                    while (tmpA > 1)
                    {
                        ret++;
                        tmpA -= 2;
                    }
                    tmpB += tmpA;
                    while (tmpB > 1)
                    {
                        ret++;
                        tmpB -= 2;
                    }
                }
                else if (favor == FavorFirstChoice.B)
                {
                    while (tmpB > 1)
                    {
                        ret++;
                        tmpA -= 2;
                    }
                    tmpA += tmpB;
                    while (tmpA > 1)
                    {
                        ret++;
                        tmpA -= 2;
                    }
                }            _goods["A"] = tmpA;
                _goods["B"] = tmpB;            return ret;
            }        private int ELargess(FavorFirstChoice favor)
            {
                int ret = 0;
                int tmp = 0;
                if (_goods.ContainsKey("B"))
                {
                    tmp = _goods["B"];
                    while (tmp > 1)
                    {
                        ret++;
                        tmp -= 2;
                    }
                }            _goods["B"] = tmp;
                return ret;
            }        private int FLargess(FavorFirstChoice favor)
            {
                int ret = 0;
                int tmp = 0;
                if (_goods.ContainsKey("C"))
                {
                    tmp = _goods["C"];
                    while (tmp > 1)
                    {
                        ret++;
                        tmp -= 2;
                    }
                }            _goods["C"] = tmp;
                return ret;
            }
      

  13.   

    汗汗...上面有一个没必要用到反射的, 改改:        private Dictionary<string, int> LargessArithmetic(string[] largessSerials, FavorFirstChoice favor)
            {
                Dictionary<string, int> ret = new Dictionary<string, int>();
                foreach (string largess in largessSerials)
                {
                    ret[largess] = LargessCalc(favor, (Largess)Enum.Parse(typeof(Largess), largess));
                }            return ret;
            }
      

  14.   

    LZ 可以重复的吗 比如 2A+ 2B 得2D ,2E,2F?
      

  15.   

    没看懂:      private int[] ok(int a, int b, int c)   //得到可选赠品数量
            {
                int[] def = new int[3];
                def[0] = (a + b) / 2;
                def[1] = b / 2;
                def[2] = c / 2;
                return def;
            }
    你这里是返回的赠品数量呢,还是可得的赠品种类?------------------------------------------------------------------------------------
    def数组代表三种赠品种类撒,而def[0]就代表d,以此类推撒。不好意思,昨天玩游戏去了=。=!!
      

  16.   

    A商品数量 + B商品数量模2为D数量
    B%2为E数量
    C%2为F数量
    列出总组合
    如果用户选择D商品则先减去对应的A数量(如果黑店可以先减B不过为意义用户怒了你站点也就黄了)当A不够再减B操作结束后如上再计算剩下的
      

  17.   

    to:jhkemail
       可能是我的能力问题,不知道我说的对否?
       我发现你的程序有两个问题
       1,我的问题中的商品只是个其中的例子,如果我有q,w,r,t....商品和规则要无限的算下去么?
       2,你的程序我运行后,为什么只能有一种选择.
       
       我的最终需求
       需要一个更灵活和通用的算法!而不是仅仅针对这个例子!  谢谢
      

  18.   

    to:jhkemail
       可能是我的能力问题,不知道我说的对否?
       我发现你的程序有两个问题
       1,我的问题中的商品只是个其中的例子,如果我有q,w,r,t....商品和规则要无限的算下去么?
       2,你的程序我运行后,为什么只能有一种选择.
       
       我的最终需求
       需要一个更灵活和通用的算法!而不是仅仅针对这个例子!  谢谢============LZ没试我写的那个方法? 应该通用了吧
      

  19.   

    to:LeoMaya
       大兄弟实在抱歉,你用到了泛型,
       可我的是vs2003,所以没试
       (惭愧还在用老掉牙的东西!哎!)
      

  20.   

    你可以把Dictionary相应改成Hashtable啊, 没关系的, 你照着那个相思就行
      

  21.   

    to:LeoMaya   谢谢你的关注有给其他的方案么?