贪心算法--C#

前言

上面总结了动态规划,下面由我来给大家介绍一下贪心算法,介绍一下贪心和动态规划有啥区别,有啥共同之处。

内容:


namespace tanxin
{
    class Program
    {
        static void Main(string[] args)
        {

            double[] w = new double[] { 30, 10, 20, 50, 40 };
            int[] v = new int[] { 65, 20, 30, 60, 40 };
            double Bage = 100;
            int n = w.Length;
            int i;
            double[] x = new double[n];
            string total = "";
            string something = "";
            double sum = 0;
            for (i = 0; i < n - 1; i++)
            {
                x[i] = 0;
            }
            for (i = 0; i < n - 1; i++)
            {
                if (w[i] <= Bage)
                {
                    x[i] = 1;
                    Bage = Bage - w[i];

                }
                else
                {
                    x[i] = Bage / w[i];
                }
                total = total + " " + x[i].ToString();
                something = something + " " + w[i].ToString();
                sum = sum + x[i] * v[i];
            }

            Console.WriteLine(total);
            Console.WriteLine(something);
            Console.WriteLine(sum);
            Console.Read();

        }






    }
}
    原文作者:贪心算法
    原文地址: https://blog.csdn.net/tgbyn/article/details/78177898
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞