已知总金额和几个商品单价,查找每个商品各取几个可以最接近或等于总价,取最优解实现程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Xml.Serialization; namespace ConsoleApp1 { class Program { static void Main( string [] args) { //10个试剂 var materialList = new List<Material>(); for ( int i = 0; i < 5; i++) { var material = new Material() { Code = i + 1, Name = "Material" + (i + 1).ToString(), Price = (i + 1), }; materialList.Add(material); material.QtyList = new List< int >(); int j = 1; //最多取20个 while (j <= 5) { material.QtyList.Add(j); j++; } } Console.WriteLine($ "MaterialList = {Newtonsoft.Json.JsonConvert.SerializeObject(materialList)}" ); decimal total = 0; int totalAmount = 70; bool flag = false ; var diffList = new List<Diff>(); plus( new Diff(), materialList, diffList, totalAmount, total, ref flag); var diff = diffList.OrderBy(p => p.DiffAmt).FirstOrDefault(); Console.ReadLine(); } public static void plus(Diff diff, List<Material> materialList, List<Diff> diffList, decimal totalAmount, decimal total, ref bool flag) { if (flag) return ; if (materialList != null && materialList.Any()) { var material = materialList.First(); var list = materialList.Where(p => p.Code != material.Code).ToList(); foreach ( var qty in material.QtyList) { if (flag) break ; if (totalAmount >= material.Price * qty + total) { var oldTotal = total; oldTotal += material.Price * qty; UpdateDiff(diff, material.Code, qty); if (list.Any()) plus(diff, list, diffList, totalAmount, oldTotal, ref flag); else { diff.DiffAmt = totalAmount - oldTotal; diffList.Add(DeepCopy(diff)); Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(diff)); if (diff.DiffAmt == 0) { flag = true ; break ; } } } } } } /// <summary> /// 深度拷贝对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <returns></returns> public static T DeepCopy<T>(T obj) { object retval; using (MemoryStream ms = new MemoryStream()) { XmlSerializer xml = new XmlSerializer( typeof (T)); xml.Serialize(ms, obj); ms.Seek(0, SeekOrigin.Begin); retval = xml.Deserialize(ms); ms.Close(); } return (T)retval; } public static void UpdateDiff(Diff diff, int index, int qty) { var propName = "Number" + index.ToString(); if (diff.GetType().GetProperty(propName) != null ) { diff.GetType().GetProperty(propName).SetValue(diff, qty); } } public static void showLog(List< int > list) { string str = string .Join( "," , list.ToArray()); Console.WriteLine(str); } } public class Material { public int Code { get ; set ; } public string Name { get ; set ; } public decimal Price { get ; set ; } public int Qty { get ; set ; } public List< int > QtyList { get ; set ; } } public class Diff { public int Number1 { get ; set ; } public int Number2 { get ; set ; } public int Number3 { get ; set ; } public int Number4 { get ; set ; } public int Number5 { get ; set ; } public int Number6 { get ; set ; } public int Number7 { get ; set ; } public int Number8 { get ; set ; } public int Number9 { get ; set ; } public int Number10 { get ; set ; } public decimal DiffAmt { get ; set ; } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)