1.4买书问题C#源码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Maishuwenti
{
class Program
{
public const int larg = 100000;

static void Main(string[] args)
{
int[] n1 = new int[] {3,4,2,1,0};
double solution = find_BestSol(n1[0],n1[1],n1[2],n1[3],n1[4]);
Console.WriteLine("最好的解决方案{0}", solution);
Console.ReadLine();
}

public static double find_BestSol(int x1, int x2, int x3, int x4, int x5)
{
int[] n = new int[5] { x1, x2, x3, x4, x5 };
rerank(ref n);
x1 = n[0];
x2 = n[1];
x3 = n[2];
x4 = n[3];
x5 = n[4];
if (n[0] > 0)
{
Console.WriteLine("n[0]>0");
return Min(8.0 + find_BestSol(x1, x2, x3, x4, x5 - 1),
2 * 8 * 0.95 + find_BestSol(x1, x2, x3, x4 - 1, x5 - 1),
3 * 8 * 0.9 + find_BestSol(x1, x2, x3 - 1, x4 - 1, x5 - 1),
4 * 8 * 0.8 + find_BestSol(x1, x2 - 1, x3 - 1, x4 - 1, x5 - 1),
5 * 8 * 0.75 + find_BestSol(x1 - 1, x2 - 1, x3 - 1, x4 - 1, x5 - 1));

}
else if ((n[0] == 0) && (n[1] > 0))
{
Console.WriteLine("n[1]>0");
return Min(8.0 + find_BestSol(x1, x2, x3, x4, x5 - 1),
2 * 8 * 0.95 + find_BestSol(x1, x2, x3, x4 - 1, x5 - 1),
3 * 8 * 0.9 + find_BestSol(x1, x2, x3 - 1, x4 - 1, x5 - 1),
4 * 8 * 0.8 + find_BestSol(x1, x2 - 1, x3 - 1, x4 - 1, x5 - 1), larg);

}
else if ((n[0] == 0) && (n[1] == 0) && (n[2] > 0))
{
Console.WriteLine("n[2]>0");
return Min(8.0 + find_BestSol(x1, x2, x3, x4, x5 - 1),
2 * 8 * 0.95 + find_BestSol(x1, x2, x3, x4 - 1, x5 - 1),
3 * 8 * 0.9 + find_BestSol(x1, x2, x3 - 1, x4 - 1, x5 - 1),
larg, larg);
}
else if ((n[0] == 0) && (n[1] == 0) && (n[2] == 0) && (n[3] > 0))
{
Console.WriteLine("n[3]>0");
return Min(8.0 + find_BestSol(x1, x2, x3, x4, x5 - 1),
2 * 8 * 0.95 + find_BestSol(x1, x2, x3, x4 - 1, x5 - 1),
larg, larg, larg);
}
else if ((n[0] == 0) && (n[1] == 0) && (n[2] == 0) && (n[3] == 0) && (n[4] > 0))
{
Console.WriteLine("n[4]>0");
return 8.0 + find_BestSol(x1, x2, x3, x4, x5 - 1);
}
else
{
Console.WriteLine("都为0");
return 0;
}
}

public static void rerank(ref int [] m)
{
//冒泡排序
for (int i = m.Length-1; i >0; i--)
{
for (int j = 0; j < i; j++)
{
if (m[j]>m[j +1])
{
int temp;
temp = m[j + 1];
m[j + 1] = m[j];
m[j] = temp;
}
}
}
}

public static double Min(double a, double b, double c, double d, double e)
{
double[] n = new double[5];
n[0] = a;
n[1] = b;
n[2] = c;
n[3] = d;
n[4] = e;
Console.WriteLine("最小值为{0}", n.Min());
return n.Min();
}
}
}
原文地址:https://www.cnblogs.com/hold/p/2310005.html