前言
上面总结了动态规划,下面由我来给大家介绍一下贪心算法,介绍一下贪心和动态规划有啥区别,有啥共同之处。
内容:
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();
}
}
}