mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-11 06:56:06 +00:00
fix(csharp): Modify method name to PascalCase, simplify new expression (#840)
* Modify method name to PascalCase(array and linked list) * Modify method name to PascalCase(backtracking) * Modify method name to PascalCase(computational complexity) * Modify method name to PascalCase(divide and conquer) * Modify method name to PascalCase(dynamic programming) * Modify method name to PascalCase(graph) * Modify method name to PascalCase(greedy) * Modify method name to PascalCase(hashing) * Modify method name to PascalCase(heap) * Modify method name to PascalCase(searching) * Modify method name to PascalCase(sorting) * Modify method name to PascalCase(stack and queue) * Modify method name to PascalCase(tree) * local check
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) {
|
||||
public int CoinChangeGreedy(int[] coins, int amt) {
|
||||
// 假设 coins 列表有序
|
||||
int i = coins.Length - 1;
|
||||
int count = 0;
|
||||
@@ -31,14 +31,14 @@ public class coin_change_greedy {
|
||||
// 贪心:能够保证找到全局最优解
|
||||
int[] coins = { 1, 5, 10, 20, 50, 100 };
|
||||
int amt = 186;
|
||||
int res = coinChangeGreedy(coins, amt);
|
||||
int res = CoinChangeGreedy(coins, amt);
|
||||
Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt);
|
||||
Console.WriteLine("凑到 " + amt + " 所需的最少硬币数量为 " + res);
|
||||
|
||||
// 贪心:无法保证找到全局最优解
|
||||
coins = new int[] { 1, 20, 50 };
|
||||
amt = 60;
|
||||
res = coinChangeGreedy(coins, amt);
|
||||
res = CoinChangeGreedy(coins, amt);
|
||||
Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt);
|
||||
Console.WriteLine("凑到 " + amt + " 所需的最少硬币数量为 " + res);
|
||||
Console.WriteLine("实际上需要的最少数量为 3 ,即 20 + 20 + 20");
|
||||
@@ -46,7 +46,7 @@ public class coin_change_greedy {
|
||||
// 贪心:无法保证找到全局最优解
|
||||
coins = new int[] { 1, 49, 50 };
|
||||
amt = 98;
|
||||
res = coinChangeGreedy(coins, amt);
|
||||
res = CoinChangeGreedy(coins, amt);
|
||||
Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt);
|
||||
Console.WriteLine("凑到 " + amt + " 所需的最少硬币数量为 " + res);
|
||||
Console.WriteLine("实际上需要的最少数量为 2 ,即 49 + 49");
|
||||
|
||||
@@ -19,7 +19,7 @@ class Item {
|
||||
|
||||
public class fractional_knapsack {
|
||||
/* 分数背包:贪心 */
|
||||
public double fractionalKnapsack(int[] wgt, int[] val, int cap) {
|
||||
public double FractionalKnapsack(int[] wgt, int[] val, int cap) {
|
||||
// 创建物品列表,包含两个属性:重量、价值
|
||||
Item[] items = new Item[wgt.Length];
|
||||
for (int i = 0; i < wgt.Length; i++) {
|
||||
@@ -51,7 +51,7 @@ public class fractional_knapsack {
|
||||
int cap = 50;
|
||||
|
||||
// 贪心算法
|
||||
double res = fractionalKnapsack(wgt, val, cap);
|
||||
double res = FractionalKnapsack(wgt, val, cap);
|
||||
Console.WriteLine("不超过背包容量的最大物品价值为 " + res);
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_greedy;
|
||||
|
||||
public class max_capacity {
|
||||
/* 最大容量:贪心 */
|
||||
public int maxCapacity(int[] ht) {
|
||||
public int MaxCapacity(int[] ht) {
|
||||
// 初始化 i, j 分列数组两端
|
||||
int i = 0, j = ht.Length - 1;
|
||||
// 初始最大容量为 0
|
||||
@@ -33,7 +33,7 @@ public class max_capacity {
|
||||
int[] ht = { 3, 8, 5, 2, 7, 7, 3, 4 };
|
||||
|
||||
// 贪心算法
|
||||
int res = maxCapacity(ht);
|
||||
int res = MaxCapacity(ht);
|
||||
Console.WriteLine("最大容量为 " + res);
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_greedy;
|
||||
|
||||
public class max_product_cutting {
|
||||
/* 最大切分乘积:贪心 */
|
||||
public int maxProductCutting(int n) {
|
||||
public int MaxProductCutting(int n) {
|
||||
// 当 n <= 3 时,必须切分出一个 1
|
||||
if (n <= 3) {
|
||||
return 1 * (n - 1);
|
||||
@@ -33,7 +33,7 @@ public class max_product_cutting {
|
||||
int n = 58;
|
||||
|
||||
// 贪心算法
|
||||
int res = maxProductCutting(n);
|
||||
int res = MaxProductCutting(n);
|
||||
Console.WriteLine("最大切分乘积为" + res);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user