mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-12 07:26:07 +00:00
feat(csharp) .NET 8.0 code migration (#966)
* .net 8.0 migration * update docs * revert change * revert change and update appendix docs * remove static * Update binary_search_insertion.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_greedy;
|
||||
|
||||
public class coin_change_greedy {
|
||||
/* 零钱兑换:贪心 */
|
||||
public int CoinChangeGreedy(int[] coins, int amt) {
|
||||
int CoinChangeGreedy(int[] coins, int amt) {
|
||||
// 假设 coins 列表有序
|
||||
int i = coins.Length - 1;
|
||||
int count = 0;
|
||||
@@ -29,14 +29,14 @@ public class coin_change_greedy {
|
||||
[Test]
|
||||
public void Test() {
|
||||
// 贪心:能够保证找到全局最优解
|
||||
int[] coins = { 1, 5, 10, 20, 50, 100 };
|
||||
int[] coins = [1, 5, 10, 20, 50, 100];
|
||||
int amt = 186;
|
||||
int res = CoinChangeGreedy(coins, amt);
|
||||
Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt);
|
||||
Console.WriteLine("凑到 " + amt + " 所需的最少硬币数量为 " + res);
|
||||
|
||||
// 贪心:无法保证找到全局最优解
|
||||
coins = new int[] { 1, 20, 50 };
|
||||
coins = [1, 20, 50];
|
||||
amt = 60;
|
||||
res = CoinChangeGreedy(coins, amt);
|
||||
Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt);
|
||||
@@ -44,7 +44,7 @@ public class coin_change_greedy {
|
||||
Console.WriteLine("实际上需要的最少数量为 3 ,即 20 + 20 + 20");
|
||||
|
||||
// 贪心:无法保证找到全局最优解
|
||||
coins = new int[] { 1, 49, 50 };
|
||||
coins = [1, 49, 50];
|
||||
amt = 98;
|
||||
res = CoinChangeGreedy(coins, amt);
|
||||
Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt);
|
||||
|
||||
@@ -7,19 +7,14 @@
|
||||
namespace hello_algo.chapter_greedy;
|
||||
|
||||
/* 物品 */
|
||||
class Item {
|
||||
public int w; // 物品重量
|
||||
public int v; // 物品价值
|
||||
|
||||
public Item(int w, int v) {
|
||||
this.w = w;
|
||||
this.v = v;
|
||||
}
|
||||
class Item(int w, int v) {
|
||||
public int w = w; // 物品重量
|
||||
public int v = v; // 物品价值
|
||||
}
|
||||
|
||||
public class fractional_knapsack {
|
||||
/* 分数背包:贪心 */
|
||||
public double FractionalKnapsack(int[] wgt, int[] val, int cap) {
|
||||
double FractionalKnapsack(int[] wgt, int[] val, int cap) {
|
||||
// 创建物品列表,包含两个属性:重量、价值
|
||||
Item[] items = new Item[wgt.Length];
|
||||
for (int i = 0; i < wgt.Length; i++) {
|
||||
@@ -46,8 +41,8 @@ public class fractional_knapsack {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] wgt = { 10, 20, 30, 40, 50 };
|
||||
int[] val = { 50, 120, 150, 210, 240 };
|
||||
int[] wgt = [10, 20, 30, 40, 50];
|
||||
int[] val = [50, 120, 150, 210, 240];
|
||||
int cap = 50;
|
||||
|
||||
// 贪心算法
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_greedy;
|
||||
|
||||
public class max_capacity {
|
||||
/* 最大容量:贪心 */
|
||||
public int MaxCapacity(int[] ht) {
|
||||
int MaxCapacity(int[] ht) {
|
||||
// 初始化 i, j 分列数组两端
|
||||
int i = 0, j = ht.Length - 1;
|
||||
// 初始最大容量为 0
|
||||
@@ -30,7 +30,7 @@ public class max_capacity {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] ht = { 3, 8, 5, 2, 7, 7, 3, 4 };
|
||||
int[] ht = [3, 8, 5, 2, 7, 7, 3, 4];
|
||||
|
||||
// 贪心算法
|
||||
int res = MaxCapacity(ht);
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_greedy;
|
||||
|
||||
public class max_product_cutting {
|
||||
/* 最大切分乘积:贪心 */
|
||||
public int MaxProductCutting(int n) {
|
||||
int MaxProductCutting(int n) {
|
||||
// 当 n <= 3 时,必须切分出一个 1
|
||||
if (n <= 3) {
|
||||
return 1 * (n - 1);
|
||||
|
||||
Reference in New Issue
Block a user