private static void FillTheKnapsack(OrderedBag<PriceWeigth> priceWeigth, int maxCapacity) { double totalPrice = 0; int lastItemIndex = 0; var priceWeigthList = priceWeigth.Reversed().ToList(); int currentLoad = 0; while (currentLoad < maxCapacity) { int crntItemWeight = priceWeigthList[lastItemIndex].Weight; if (currentLoad + crntItemWeight <= maxCapacity) { totalPrice += priceWeigthList[lastItemIndex].Price; lastItemIndex++; currentLoad += crntItemWeight; } else { double pricePortion = (maxCapacity - currentLoad) / (double)crntItemWeight; totalPrice += (priceWeigthList[lastItemIndex].Price * pricePortion); currentLoad += crntItemWeight; } } Console.WriteLine("Total price = {0:f2}", totalPrice); }
static void Main(string[] args) { OrderedBag <int> ob = new OrderedBag <int>(); ob.Add(7); ob.Add(5); ob.Add(5); ob.Add(5); ob.Add(1); Console.WriteLine(ob.Reversed()); }
private static void ShowMostCalled( TraceFile file, int count, GetNameDelegate getName) { Dictionary <string, RankingItem> routines = new Dictionary <string, RankingItem>(); uint totalCount = 0; // // Build histogram. // foreach (TraceClient client in file.Clients) { Traverse(client.Calls, routines, ref totalCount, getName); } // // Create ranking. // OrderedBag <RankingItem> ranking = new OrderedBag <RankingItem>(); foreach (RankingItem item in routines.Values) { ranking.Add(item); } int index = 0; foreach (RankingItem item in ranking.Reversed()) { if (index++ == count) { break; } Console.WriteLine("{0,60}\t{1}", item.FunctionName, item.CallCount); } Console.WriteLine(); Console.WriteLine("{0} events total", totalCount); }