CSharp: Enumeration Algorithms

CSharp: Enumeration Algorithms /* # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述Enumeration Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : vs2026 c# .net 10 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/07/05 22:16 # User : geovindu # Product : Visual Studio 2026 # Project : CSharpDesignPattern # File : RecommendEnum.cs */ using System; using System.Collections.Generic; using System.Text; namespace CSharpAlgorithms.Enumeration { /// summary /// 珠宝商品实体 /// /summary public class Jewelry { public string JID { get; set; } public string Category { get; set; } public string Material { get; set; } public decimal Price { get; set; } public int Stock { get; set; } public int Sales { get; set; } public Liststring SceneTags { get; set; } /// summary /// 计算折后价 /// /summary /// param namediscount/param /// returns/returns public decimal GetDiscountPrice(decimal discount) { return Price * discount; } /// summary /// /// /summary /// returns/returns public override string ToString() { var scenes string.Join(,, SceneTags); return $【{JID}】{Category} | {Material} | 原价{Price:F0}元 | 库存{Stock}件 | 销量{Sales} | 适用场景{scenes}; } } /// summary /// 带评分的推荐单品 /// /summary public class RecommendItem { public int Score { get; set; } public Jewelry Goods { get; set; } } /// summary /// 两件套装项链戒指 /// /summary public class ComboTwo { public Jewelry Necklace { get; set; } public Jewelry Ring { get; set; } public decimal Total { get; set; } } /// summary /// 三金套装项链手镯戒指三层循环枚举 /// /summary public class ComboThree { public Jewelry Necklace { get; set; } public Jewelry Bracelet { get; set; } public Jewelry Ring { get; set; } public decimal Total { get; set; } } /// summary /// 五金套装项链手镯戒指耳饰吊坠五层循环枚举 /// /summary public class ComboFive { public Jewelry Necklace { get; set; } public Jewelry Bracelet { get; set; } public Jewelry Ring { get; set; } public Jewelry Earrings { get; set; } public Jewelry Pendant { get; set; } public decimal Total { get; set; } } public class JewelryService { /// summary /// 单品枚举筛选 打分排序 /// /summary /// param namegoodsList全店商品/param /// param namemaxBudget最高预算/param /// param namediscount折扣系数 0~1 如0.9595折/param /// param nametargetMaterial指定材质空不限/param /// param nametargetCategory指定品类空不限/param /// param nametargetScene指定场景空不限/param /// returns主推款(预算内),升级款(小幅超预算≤120%)/returns public TupleListJewelry, ListJewelry EnumerateFilter( ListJewelry goodsList, decimal maxBudget, decimal discount, string targetMaterial, string targetCategory, string targetScene) { ListRecommendItem candidates new ListRecommendItem(); foreach (var item in goodsList) { // 无库存直接跳过 if (item.Stock 1) continue; decimal realPrice item.GetDiscountPrice(discount); // 超过预算120%直接过滤 if (realPrice maxBudget * 1.2m) continue; // 材质筛选 if (!string.IsNullOrEmpty(targetMaterial) item.Material ! targetMaterial) continue; // 品类筛选 if (!string.IsNullOrEmpty(targetCategory) item.Category ! targetCategory) continue; int score 0; decimal priceRatio realPrice / maxBudget; // 价格打分 if (priceRatio 0.7m priceRatio 0.9m) score 50; else if (priceRatio 1.0m) score 30; else score 10; // 场景匹配加分 if (!string.IsNullOrEmpty(targetScene) item.SceneTags.Contains(targetScene)) score 30; // 销量加分上限20 int saleScore item.Sales / 10; if (saleScore 20) saleScore 20; score saleScore; candidates.Add(new RecommendItem { Score score, Goods item }); } // 按分数降序排序 var sorted candidates.OrderByDescending(x x.Score).ToList(); ListJewelry mainList new ListJewelry(); ListJewelry upgradeList new ListJewelry(); foreach (var c in sorted) { decimal realPrice c.Goods.GetDiscountPrice(discount); if (realPrice maxBudget) mainList.Add(c.Goods); else upgradeList.Add(c.Goods); } return Tuple.Create(mainList, upgradeList); } /// summary /// 两层循环项链戒指两件套枚举 /// /summary /// param namegoodsList/param /// param nametotalBudget/param /// param namediscount/param /// returns/returns public ListComboTwo EnumerateTwoCombo(ListJewelry goodsList, decimal totalBudget, decimal discount) { ListComboTwo result new ListComboTwo(); var necklaces goodsList.Where(g g.Stock 0 g.Category 项链).ToList(); var rings goodsList.Where(g g.Stock 0 g.Category 戒指).ToList(); foreach (var n in necklaces) { foreach (var r in rings) { decimal sum n.GetDiscountPrice(discount) r.GetDiscountPrice(discount); if (sum totalBudget) { result.Add(new ComboTwo { Necklace n, Ring r, Total sum }); } } } return result; } /// summary /// 三层循环婚嫁三金 项链手镯戒指 /// /summary /// param namegoodsList/param /// param nametotalBudget/param /// param namediscount/param /// returns/returns public ListComboThree EnumerateThreeCombo(ListJewelry goodsList, decimal totalBudget, decimal discount) { ListComboThree result new ListComboThree(); var necklaces goodsList.Where(g g.Stock 0 g.Category 项链).ToList(); var bracelets goodsList.Where(g g.Stock 0 g.Category 手镯).ToList(); var rings goodsList.Where(g g.Stock 0 g.Category 戒指).ToList(); // 三层嵌套枚举全部组合 foreach (var n in necklaces) { foreach (var b in bracelets) { foreach (var r in rings) { decimal sum n.GetDiscountPrice(discount) b.GetDiscountPrice(discount) r.GetDiscountPrice(discount); if (sum totalBudget) { result.Add(new ComboThree { Necklace n, Bracelet b, Ring r, Total sum }); } } } } return result; } /// summary /// 五层循环婚嫁五金 项链手镯戒指耳饰吊坠 /// /summary /// param namegoodsList/param /// param nametotalBudget/param /// param namediscount/param /// returns/returns public ListComboFive EnumerateFiveCombo(ListJewelry goodsList, decimal totalBudget, decimal discount) { ListComboFive result new ListComboFive(); var necklaces goodsList.Where(g g.Stock 0 g.Category 项链).ToList(); var bracelets goodsList.Where(g g.Stock 0 g.Category 手镯).ToList(); var rings goodsList.Where(g g.Stock 0 g.Category 戒指).ToList(); var earrings goodsList.Where(g g.Stock 0 g.Category 耳饰).ToList(); var pendants goodsList.Where(g g.Stock 0 g.Category 吊坠).ToList(); // 五层嵌套枚举全部五金搭配 foreach (var n in necklaces) { foreach (var b in bracelets) { foreach (var r in rings) { foreach (var e in earrings) { foreach (var p in pendants) { decimal sum n.GetDiscountPrice(discount) b.GetDiscountPrice(discount) r.GetDiscountPrice(discount) e.GetDiscountPrice(discount) p.GetDiscountPrice(discount); if (sum totalBudget) { result.Add(new ComboFive { Necklace n, Bracelet b, Ring r, Earrings e, Pendant p, Total sum }); } } } } } } return result; } /// summary /// 初始化门店商品数据 /// /summary public ListJewelry InitStoreData() { return new ListJewelry() { new Jewelry{JIDN001,Category项链,Material黄金,Price5280,Stock12,Sales120,SceneTagsnew Liststring{婚嫁,送礼}}, new Jewelry{JIDN002,Category项链,Material铂金,Price7600,Stock3,Sales60,SceneTagsnew Liststring{求婚,日常}}, new Jewelry{JIDN003,Category项链,Material钻石,Price12800,Stock5,Sales45,SceneTagsnew Liststring{纪念日}}, new Jewelry{JIDN004,Category项链,MaterialK金,Price3680,Stock8,Sales150,SceneTagsnew Liststring{日常}}, new Jewelry{JIDR001,Category戒指,Material黄金,Price2150,Stock15,Sales200,SceneTagsnew Liststring{日常,婚嫁}}, new Jewelry{JIDR002,Category戒指,Material钻石,Price9999,Stock2,Sales88,SceneTagsnew Liststring{求婚}}, new Jewelry{JIDR003,Category戒指,Material银饰,Price599,Stock30,Sales320,SceneTagsnew Liststring{日常}}, new Jewelry{JIDB001,Category手镯,Material黄金,Price8600,Stock4,Sales80,SceneTagsnew Liststring{婚嫁,送礼}}, new Jewelry{JIDB002,Category手镯,Material银饰,Price1280,Stock22,Sales260,SceneTagsnew Liststring{日常}}, new Jewelry{JIDE001,Category耳饰,MaterialK金,Price1680,Stock18,Sales190,SceneTagsnew Liststring{日常,纪念日}}, new Jewelry{JIDE002,Category耳饰,Material铂金,Price4200,Stock6,Sales72,SceneTagsnew Liststring{求婚}}, new Jewelry{JIDE003,Category耳饰,Material钻石,Price6500,Stock0,Sales30,SceneTagsnew Liststring{纪念日}}, new Jewelry{JIDP001,Category吊坠,Material黄金,Price2600,Stock9,Sales110,SceneTagsnew Liststring{婚嫁,送礼}}, new Jewelry{JIDP002,Category吊坠,MaterialK金,Price1200,Stock14,Sales170,SceneTagsnew Liststring{日常}} }; } } } /* # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述Enumeration Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : vs2026 c# .net 10 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/07/05 22:16 # User : geovindu # Product : Visual Studio 2026 # Project : CSharpDesignPattern # File : EnumerationBll.cs */ using CSharpAlgorithms.Enumeration; using System; using System.Collections.Generic; using System.Text; namespace CSharpAlgorithms.Bll { /// summary /// /// /summary public class EnumerationBll { /// summary /// /// /summary public void Demo() { JewelryService service new JewelryService(); ListJewelry allGoods service.InitStoreData(); decimal discount 0.95m; // 全场95折 Console.WriteLine( 珠宝门店全部商品枚举全集全场95折 ); foreach (var item in allGoods) { Console.WriteLine(${item} 折后价{item.GetDiscountPrice(discount):F0}元); } Console.WriteLine(new string(-, 110)); // 场景1单品推荐 预算6000不限材质品类场景 Console.WriteLine(\n【顾客需求1】单品推荐预算≤6000元不限材质品类全场95折); var filterResult service.EnumerateFilter(allGoods, 6000, discount, , , ); ListJewelry mainList filterResult.Item1; ListJewelry upgradeList filterResult.Item2; if (mainList.Count 0) Console.WriteLine(无符合条件单品); else { foreach (var g in mainList) { Console.WriteLine(${g} 折后价{g.GetDiscountPrice(discount):F0}元); } } // 场景2两件套 项链戒指组合预算10000 Console.WriteLine(\n 两件套装项链戒指总价≤1000095折 ); var twoCombo service.EnumerateTwoCombo(allGoods, 10000, discount); if (twoCombo.Count 0) Console.WriteLine(无两件套组合); else { foreach (var c in twoCombo) { Console.WriteLine($套装组合); Console.WriteLine($ {c.Necklace}); Console.WriteLine($ {c.Ring}); Console.WriteLine($ 折后合计{c.Total:F0}元); Console.WriteLine(----------------------------------------); } } // 场景3三层循环 婚嫁三金项链手镯戒指预算16000 Console.WriteLine(\n 婚嫁三金套装三层循环枚举总价≤1600095折 ); var threeCombo service.EnumerateThreeCombo(allGoods, 16000, discount); if (threeCombo.Count 0) Console.WriteLine(无三金组合); else { foreach (var c in threeCombo) { Console.WriteLine($三金套装); Console.WriteLine($ {c.Necklace}); Console.WriteLine($ {c.Bracelet}); Console.WriteLine($ {c.Ring}); Console.WriteLine($ 折后合计{c.Total:F0}元); Console.WriteLine(----------------------------------------); } } // 场景4五层循环 婚嫁五金 预算22000 Console.WriteLine(\n 婚嫁五金套装五层循环枚举总价≤2200095折 ); var fiveCombo service.EnumerateFiveCombo(allGoods, 22000, discount); if (fiveCombo.Count 0) Console.WriteLine(无五金组合); else { foreach (var c in fiveCombo) { Console.WriteLine($五金套装); Console.WriteLine($ {c.Necklace}); Console.WriteLine($ {c.Bracelet}); Console.WriteLine($ {c.Ring}); Console.WriteLine($ {c.Earrings}); Console.WriteLine($ {c.Pendant}); Console.WriteLine($ 折后合计{c.Total:F0}元); Console.WriteLine(----------------------------------------); } } // 小幅超预算升级单品展示 Console.WriteLine(\n【预算6000升级备选单品小幅超预算】); foreach (var g in upgradeList) { Console.WriteLine(${g} 折后价{g.GetDiscountPrice(discount):F0}元); } Console.ReadLine(); } } }介绍了一个珠宝商品推荐系统的C#实现包含单品推荐和套装组合功能。系统通过枚举算法实现Jewelry类定义珠宝商品属性和折扣计算JewelryService提供核心算法EnumerateFilter()单品筛选打分排序EnumerateTwoCombo()项链戒指两件套组合EnumerateThreeCombo()三金套装组合EnumerateFiveCombo()五金套装组合业务逻辑层EnumerationBll演示了单品推荐预算6000元两件套预算1万元三金套装预算1.6万元五金套装预算2.2万元系统支持价格折扣、库存检查、场景标签匹配等功能通过多层循环枚举所有可能组合输出符合预算的推荐方案。输出