mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-28 07:21:55 +08:00
Correct C# knapsack ordering, repeated keys and heap output (#1998)
Co-authored-by: Lukas <103962359+L4XB@users.noreply.github.com>
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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 [];
|
||||
}
|
||||
|
||||
@@ -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<int?>().ToList());
|
||||
PrintTree(tree);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 [];
|
||||
}
|
||||
|
||||
@@ -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<int?>().ToList());
|
||||
PrintTree(tree);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 [];
|
||||
}
|
||||
|
||||
@@ -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<int?>().ToList());
|
||||
PrintTree(tree);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 [];
|
||||
}
|
||||
|
||||
@@ -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<int?>().ToList());
|
||||
PrintTree(tree);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 [];
|
||||
}
|
||||
|
||||
@@ -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<int?>().ToList());
|
||||
PrintTree(tree);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 [];
|
||||
}
|
||||
|
||||
@@ -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<int?>().ToList());
|
||||
PrintTree(tree);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user