编辑代码

using System;
using System.Collections.Generic;

public class HelloWorld
{
     // 背包问题的蛮力算法
    static int BruteForceKnapsack(List<int> weights, List<int> values, int capacity)
    {
        int n = weights.Count;
        int maxProfit = 0;

        for (int i = 0; i < (1 << n); i++)
        {
            int currentWeight = 0;
            int currentProfit = 0;

            for (int j = 0; j < n; j++)
            {
                if ((i & (1 << j)) != 0)
                {
                    currentWeight += weights[j];
                    currentProfit += values[j];
                }
            }

            if (currentWeight <= capacity && currentProfit > maxProfit)
            {
                maxProfit = currentProfit;
            }
        }

        return maxProfit;
    }

    static void Main()
    {
        List<int> weights = new List<int> { 2, 3, 4, 5 };
        List<int> values = new List<int> { 3, 4, 5, 6 };
        int capacity = 5;

        int result = BruteForceKnapsack(weights, values, capacity);

        Console.WriteLine($"背包问题的最大价值:{result}");
    }
}