Files
hello-algo/ru/codes/csharp/chapter_greedy/fractional_knapsack.cs
Yudong Jin 772183705e Add ru version (#1865)
* Add Russian docs site baseline

* Add Russian localized codebase

* Polish Russian code wording

* Update ru code translation.

* Update code translation and chapter covers.

* Fix pythontutor extraction.

* Add README and landing page.

* placeholder of profiles

* Use figures of English version

* Remove chapter paperbook
2026-03-28 04:24:07 +08:00

52 lines
2.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* File: fractional_knapsack.cs
* Created Time: 2023-07-21
* Author: hpstory (hpstory1024@163.com)
*/
namespace hello_algo.chapter_greedy;
/* Предмет */
class Item(int w, int v) {
public int w = w; // Вес предмета
public int v = v; // Стоимость предмета
}
public class fractional_knapsack {
/* Дробный рюкзак: жадный алгоритм */
double FractionalKnapsack(int[] wgt, int[] val, int cap) {
// Создать список предметов с двумя свойствами: вес и стоимость
Item[] items = new Item[wgt.Length];
for (int i = 0; i < wgt.Length; i++) {
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));
// Циклический жадный выбор
double res = 0;
foreach (Item item in items) {
if (item.w <= cap) {
// Если оставшейся вместимости достаточно, положить в рюкзак текущий предмет целиком
res += item.v;
cap -= item.w;
} else {
// Если оставшейся вместимости недостаточно, положить в рюкзак часть текущего предмета
res += (double)item.v / item.w * cap;
// Свободной вместимости больше не осталось, поэтому выйти из цикла
break;
}
}
return res;
}
[Test]
public void Test() {
int[] wgt = [10, 20, 30, 40, 50];
int[] val = [50, 120, 150, 210, 240];
int cap = 50;
// Жадный алгоритм
double res = FractionalKnapsack(wgt, val, cap);
Console.WriteLine("Максимальная стоимость предметов без превышения вместимости рюкзака = " + res);
}
}