Exemple #1
0
        public void CalculateTest()
        {
            //a:500,b:195,c:105,d:100
            var product = new List <Product>()
            {
                new Product("a", 500),
                new Product("b", 195),
                new Product("c", 105),
                new Product("d", 100),
            };
            var result = DynamicCalculate.GetOptimalCombination(300, 10, product);

            Assert.AreEqual(1, result.Count);

            var spell   = new SpellAllocation(product, 300, 10);
            var result2 = spell.GetOptimalCombination();

            CheckAlgorithm(result2, result);
        }
Exemple #2
0
        public void CalculateTest2()
        {
            //a:500,b:195,c:105,d:100
            var product = new List <Product>()
            {
                new Product("内衣", 28.8m),
                new Product("盆子", 32.9m),
                new Product("架子", 62.1m),
                new Product("靴子", 1214),
                new Product("智能锁", 4019),
                new Product("擦脸", 216)
            };
            var result = DynamicCalculate.GetOptimalCombination(300, 20, product);

            Assert.AreEqual(3, result.Count);

            var spell   = new SpellAllocation(product, 300, 20);
            var result2 = spell.GetOptimalCombination();

            CheckAlgorithm(result2, result);
        }
Exemple #3
0
        static void Main(string[] args)
        {
            var input = "内衣:28.8,盆子:32.9,架子:62.1,靴子:1214,智能锁:4019,擦脸:216".Replace(":", ":").Replace(",", ",");

            do
            {
                var list = new List <Product>();

                Console.WriteLine("请输入 商品:价格,以逗号分隔。例如:内衣:28.8,盆子:32.9,架子:62.1,靴子:1214");
                try
                {
                    input = Console.ReadLine().Replace(":", ":").Replace(",", ",");
                    var products = input.Split(",");
                    foreach (var prod in products)
                    {
                        var     arr = prod.Split(":");
                        Product prd = new Product(arr[0], Convert.ToDecimal(arr[1]));
                        list.Add(prd);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("输入格式有问题,请确认。");
                }
                var result = DynamicCalculate.Calculate(300, 20, list);
                foreach (var allocation in result)
                {
                    var index = result.IndexOf(allocation);
                    Console.WriteLine();
                    Console.WriteLine($"{index,4}  {allocation}");
                }
            } while (input != "exist");


            Console.Read();
        }