From 61ad6ed7f6b30b25e54c580ba3cf3cc84aff63b2 Mon Sep 17 00:00:00 2001 From: Lukas <103962359+L4XB@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:27:02 +0100 Subject: [PATCH] Correct C# knapsack ordering, repeated keys and heap output (#1998) Co-authored-by: Lukas <103962359+L4XB@users.noreply.github.com> --- codes/csharp/chapter_greedy/fractional_knapsack.cs | 2 +- codes/csharp/chapter_searching/two_sum.cs | 2 +- codes/csharp/utils/PrintUtil.cs | 2 +- en/codes/csharp/chapter_greedy/fractional_knapsack.cs | 2 +- en/codes/csharp/chapter_searching/two_sum.cs | 2 +- en/codes/csharp/utils/PrintUtil.cs | 2 +- ja/codes/csharp/chapter_greedy/fractional_knapsack.cs | 2 +- ja/codes/csharp/chapter_searching/two_sum.cs | 2 +- ja/codes/csharp/utils/PrintUtil.cs | 2 +- ko/codes/csharp/chapter_greedy/fractional_knapsack.cs | 2 +- ko/codes/csharp/chapter_searching/two_sum.cs | 2 +- ko/codes/csharp/utils/PrintUtil.cs | 2 +- ru/codes/csharp/chapter_greedy/fractional_knapsack.cs | 2 +- ru/codes/csharp/chapter_searching/two_sum.cs | 2 +- ru/codes/csharp/utils/PrintUtil.cs | 2 +- zh-hant/codes/csharp/chapter_greedy/fractional_knapsack.cs | 2 +- zh-hant/codes/csharp/chapter_searching/two_sum.cs | 2 +- zh-hant/codes/csharp/utils/PrintUtil.cs | 2 +- 18 files changed, 18 insertions(+), 18 deletions(-) diff --git a/codes/csharp/chapter_greedy/fractional_knapsack.cs b/codes/csharp/chapter_greedy/fractional_knapsack.cs index e2f477b286..81d714a41a 100644 --- a/codes/csharp/chapter_greedy/fractional_knapsack.cs +++ b/codes/csharp/chapter_greedy/fractional_knapsack.cs @@ -21,7 +21,7 @@ public class fractional_knapsack { items[i] = new Item(wgt[i], val[i]); } // 按照单位价值 item.v / item.w 从高到低进行排序 - Array.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w)); + Array.Sort(items, (x, y) => ((double)y.v / y.w).CompareTo((double)x.v / x.w)); // 循环贪心选择 double res = 0; foreach (Item item in items) { diff --git a/codes/csharp/chapter_searching/two_sum.cs b/codes/csharp/chapter_searching/two_sum.cs index d463eeb218..0541d0d642 100644 --- a/codes/csharp/chapter_searching/two_sum.cs +++ b/codes/csharp/chapter_searching/two_sum.cs @@ -30,7 +30,7 @@ public class two_sum { if (dic.ContainsKey(target - nums[i])) { return [dic[target - nums[i]], i]; } - dic.Add(nums[i], i); + dic[nums[i]] = i; } return []; } diff --git a/codes/csharp/utils/PrintUtil.cs b/codes/csharp/utils/PrintUtil.cs index b91f013251..e897a96062 100644 --- a/codes/csharp/utils/PrintUtil.cs +++ b/codes/csharp/utils/PrintUtil.cs @@ -124,8 +124,8 @@ public static class PrintUtil { while (newQueue.TryDequeue(out int element, out _)) { list.Add(element); } - Console.WriteLine("堆的树状表示:"); Console.WriteLine(string.Join(',', list.ToList())); + Console.WriteLine("堆的树状表示:"); TreeNode? tree = TreeNode.ListToTree(list.Cast().ToList()); PrintTree(tree); } diff --git a/en/codes/csharp/chapter_greedy/fractional_knapsack.cs b/en/codes/csharp/chapter_greedy/fractional_knapsack.cs index 8db7ede927..b92f2b451e 100644 --- a/en/codes/csharp/chapter_greedy/fractional_knapsack.cs +++ b/en/codes/csharp/chapter_greedy/fractional_knapsack.cs @@ -21,7 +21,7 @@ public class fractional_knapsack { items[i] = new Item(wgt[i], val[i]); } // Sort by value per unit weight, item.v / item.w, in descending order - Array.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w)); + Array.Sort(items, (x, y) => ((double)y.v / y.w).CompareTo((double)x.v / x.w)); // Repeatedly make greedy choices double res = 0; foreach (Item item in items) { diff --git a/en/codes/csharp/chapter_searching/two_sum.cs b/en/codes/csharp/chapter_searching/two_sum.cs index 96d50944fa..f9d13dd08a 100644 --- a/en/codes/csharp/chapter_searching/two_sum.cs +++ b/en/codes/csharp/chapter_searching/two_sum.cs @@ -30,7 +30,7 @@ public class two_sum { if (dic.ContainsKey(target - nums[i])) { return [dic[target - nums[i]], i]; } - dic.Add(nums[i], i); + dic[nums[i]] = i; } return []; } diff --git a/en/codes/csharp/utils/PrintUtil.cs b/en/codes/csharp/utils/PrintUtil.cs index 74a9dcc4d2..686235a961 100644 --- a/en/codes/csharp/utils/PrintUtil.cs +++ b/en/codes/csharp/utils/PrintUtil.cs @@ -124,8 +124,8 @@ public static class PrintUtil { while (newQueue.TryDequeue(out int element, out _)) { list.Add(element); } - Console.WriteLine("Tree representation of the heap:"); Console.WriteLine(string.Join(',', list.ToList())); + Console.WriteLine("Tree representation of the heap:"); TreeNode? tree = TreeNode.ListToTree(list.Cast().ToList()); PrintTree(tree); } diff --git a/ja/codes/csharp/chapter_greedy/fractional_knapsack.cs b/ja/codes/csharp/chapter_greedy/fractional_knapsack.cs index 8e660d9623..f9f111af8b 100644 --- a/ja/codes/csharp/chapter_greedy/fractional_knapsack.cs +++ b/ja/codes/csharp/chapter_greedy/fractional_knapsack.cs @@ -21,7 +21,7 @@ public class fractional_knapsack { items[i] = new Item(wgt[i], val[i]); } // 単位重さ当たりの価値 item.v / item.w の降順にソートする - Array.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w)); + Array.Sort(items, (x, y) => ((double)y.v / y.w).CompareTo((double)x.v / x.w)); // 貪欲な選択を繰り返す double res = 0; foreach (Item item in items) { diff --git a/ja/codes/csharp/chapter_searching/two_sum.cs b/ja/codes/csharp/chapter_searching/two_sum.cs index bd96a84a94..decce67b79 100644 --- a/ja/codes/csharp/chapter_searching/two_sum.cs +++ b/ja/codes/csharp/chapter_searching/two_sum.cs @@ -30,7 +30,7 @@ public class two_sum { if (dic.ContainsKey(target - nums[i])) { return [dic[target - nums[i]], i]; } - dic.Add(nums[i], i); + dic[nums[i]] = i; } return []; } diff --git a/ja/codes/csharp/utils/PrintUtil.cs b/ja/codes/csharp/utils/PrintUtil.cs index b9eb63ad78..da06f728d9 100644 --- a/ja/codes/csharp/utils/PrintUtil.cs +++ b/ja/codes/csharp/utils/PrintUtil.cs @@ -124,8 +124,8 @@ public static class PrintUtil { while (newQueue.TryDequeue(out int element, out _)) { list.Add(element); } - Console.WriteLine("ヒープの木構造表現:"); Console.WriteLine(string.Join(',', list.ToList())); + Console.WriteLine("ヒープの木構造表現:"); TreeNode? tree = TreeNode.ListToTree(list.Cast().ToList()); PrintTree(tree); } diff --git a/ko/codes/csharp/chapter_greedy/fractional_knapsack.cs b/ko/codes/csharp/chapter_greedy/fractional_knapsack.cs index ec1358fcbe..dd945fca7c 100644 --- a/ko/codes/csharp/chapter_greedy/fractional_knapsack.cs +++ b/ko/codes/csharp/chapter_greedy/fractional_knapsack.cs @@ -21,7 +21,7 @@ public class fractional_knapsack { items[i] = new Item(wgt[i], val[i]); } // 단위 가치 item.v / item.w를 기준으로 내림차순 정렬한다 - Array.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w)); + Array.Sort(items, (x, y) => ((double)y.v / y.w).CompareTo((double)x.v / x.w)); // 탐욕적 선택을 반복한다 double res = 0; foreach (Item item in items) { diff --git a/ko/codes/csharp/chapter_searching/two_sum.cs b/ko/codes/csharp/chapter_searching/two_sum.cs index 26d9d00a50..6dc695392c 100644 --- a/ko/codes/csharp/chapter_searching/two_sum.cs +++ b/ko/codes/csharp/chapter_searching/two_sum.cs @@ -30,7 +30,7 @@ public class two_sum { if (dic.ContainsKey(target - nums[i])) { return [dic[target - nums[i]], i]; } - dic.Add(nums[i], i); + dic[nums[i]] = i; } return []; } diff --git a/ko/codes/csharp/utils/PrintUtil.cs b/ko/codes/csharp/utils/PrintUtil.cs index 82ebdf3e5b..e62b860487 100644 --- a/ko/codes/csharp/utils/PrintUtil.cs +++ b/ko/codes/csharp/utils/PrintUtil.cs @@ -124,8 +124,8 @@ public static class PrintUtil { while (newQueue.TryDequeue(out int element, out _)) { list.Add(element); } - Console.WriteLine("힙의 트리 표현:"); Console.WriteLine(string.Join(',', list.ToList())); + Console.WriteLine("힙의 트리 표현:"); TreeNode? tree = TreeNode.ListToTree(list.Cast().ToList()); PrintTree(tree); } diff --git a/ru/codes/csharp/chapter_greedy/fractional_knapsack.cs b/ru/codes/csharp/chapter_greedy/fractional_knapsack.cs index 3b2f48bd28..3920e86432 100644 --- a/ru/codes/csharp/chapter_greedy/fractional_knapsack.cs +++ b/ru/codes/csharp/chapter_greedy/fractional_knapsack.cs @@ -21,7 +21,7 @@ public class fractional_knapsack { items[i] = new Item(wgt[i], val[i]); } // Отсортировать по удельной стоимости item.v / item.w в порядке убывания - Array.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w)); + Array.Sort(items, (x, y) => ((double)y.v / y.w).CompareTo((double)x.v / x.w)); // Циклический жадный выбор double res = 0; foreach (Item item in items) { diff --git a/ru/codes/csharp/chapter_searching/two_sum.cs b/ru/codes/csharp/chapter_searching/two_sum.cs index 9a3c8e6a96..6ae54f56b2 100644 --- a/ru/codes/csharp/chapter_searching/two_sum.cs +++ b/ru/codes/csharp/chapter_searching/two_sum.cs @@ -30,7 +30,7 @@ public class two_sum { if (dic.ContainsKey(target - nums[i])) { return [dic[target - nums[i]], i]; } - dic.Add(nums[i], i); + dic[nums[i]] = i; } return []; } diff --git a/ru/codes/csharp/utils/PrintUtil.cs b/ru/codes/csharp/utils/PrintUtil.cs index 794c6a0e92..1ffb18d171 100644 --- a/ru/codes/csharp/utils/PrintUtil.cs +++ b/ru/codes/csharp/utils/PrintUtil.cs @@ -124,8 +124,8 @@ public static class PrintUtil { while (newQueue.TryDequeue(out int element, out _)) { list.Add(element); } - Console.WriteLine("Древовидное представление кучи:"); Console.WriteLine(string.Join(',', list.ToList())); + Console.WriteLine("Древовидное представление кучи:"); TreeNode? tree = TreeNode.ListToTree(list.Cast().ToList()); PrintTree(tree); } diff --git a/zh-hant/codes/csharp/chapter_greedy/fractional_knapsack.cs b/zh-hant/codes/csharp/chapter_greedy/fractional_knapsack.cs index d88bb3e81f..f6e80810f2 100644 --- a/zh-hant/codes/csharp/chapter_greedy/fractional_knapsack.cs +++ b/zh-hant/codes/csharp/chapter_greedy/fractional_knapsack.cs @@ -21,7 +21,7 @@ public class fractional_knapsack { items[i] = new Item(wgt[i], val[i]); } // 按照單位價值 item.v / item.w 從高到低進行排序 - Array.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w)); + Array.Sort(items, (x, y) => ((double)y.v / y.w).CompareTo((double)x.v / x.w)); // 迴圈貪婪選擇 double res = 0; foreach (Item item in items) { diff --git a/zh-hant/codes/csharp/chapter_searching/two_sum.cs b/zh-hant/codes/csharp/chapter_searching/two_sum.cs index 2c60ce7e49..9e5e3e751f 100644 --- a/zh-hant/codes/csharp/chapter_searching/two_sum.cs +++ b/zh-hant/codes/csharp/chapter_searching/two_sum.cs @@ -30,7 +30,7 @@ public class two_sum { if (dic.ContainsKey(target - nums[i])) { return [dic[target - nums[i]], i]; } - dic.Add(nums[i], i); + dic[nums[i]] = i; } return []; } diff --git a/zh-hant/codes/csharp/utils/PrintUtil.cs b/zh-hant/codes/csharp/utils/PrintUtil.cs index 538866991e..db99798eed 100644 --- a/zh-hant/codes/csharp/utils/PrintUtil.cs +++ b/zh-hant/codes/csharp/utils/PrintUtil.cs @@ -124,8 +124,8 @@ public static class PrintUtil { while (newQueue.TryDequeue(out int element, out _)) { list.Add(element); } - Console.WriteLine("堆積的樹狀表示:"); Console.WriteLine(string.Join(',', list.ToList())); + Console.WriteLine("堆積的樹狀表示:"); TreeNode? tree = TreeNode.ListToTree(list.Cast().ToList()); PrintTree(tree); }