我有以下的解决,我不知道如何处理这样的:找到最好的情况下C#
有停车场是彼此相邻和它们的位置酷似一条直线。每个停车场都有一个值(利润)分配给它。您可以根据需要购买尽可能多的批次,但它们必须彼此相邻(在一组连续的集合中)。
INPUT(这是所给/你会类型):
数量很多:9
值对于每个停车场:即:-5,0,7,-6,4, 3,-5,0,2
表示(为了容易观看) 每个盒子包含每个大量的利润:
OUTPUT: 应该是:含义: 3 - 开始批次#, 6 - 结束批号, 8 - 利润总额(7 - 6 + 4 + 3)
如果有一个以上的答案,程序应该写出包含最少停车位数量的程序。如果还有不止一个可能的答案,你的程序可以写任何一个答案。
请帮助。提前致谢。
编辑: 我得到它的工作:
/// <summary>
/// The problem 2.
/// </summary>
public class MySuperAwesomeClass
{
#region Constants and Fields
/// <summary>
/// The seq end.
/// </summary>
private static int seqEnd = -1;
/// <summary>
/// The seq start.
/// </summary>
private static int seqStart;
#endregion
// Quadratic maximum contiguous subsequence sum algorithm.
#region Public Methods and Operators
/// <summary>
/// The max sub sum 2.
/// </summary>
/// <param name="a">
/// The a.
/// </param>
/// <returns>
/// The max sub sum 2.
/// </returns>
public static int maxSumSub(int[] a)
{
int maxSum = 0;
for (int i = 0; i < a.Length; i++)
{
int thisSum = 0;
for (int j = i; j < a.Length; j++)
{
thisSum += a[j];
if (thisSum > maxSum)
{
maxSum = thisSum;
seqStart = i;
seqEnd = j;
}
}
}
return maxSum;
}
#endregion
#region Methods
/// <summary>
/// The main.
/// </summary>
private static void Main()
{
Console.WriteLine("Enter N:");
string stringInput = Console.ReadLine();
int[] a = new int[Convert.ToInt16(stringInput)];
Console.WriteLine("Enter profit values:");
for (int i = 0; i < Convert.ToInt16(stringInput); i++)
{
string value = Console.ReadLine();
a[i] = Convert.ToInt16(value);
}
int maxSum = maxSumSub(a);
Console.WriteLine(string.Format("{0} {1} {2}", seqStart, seqEnd, maxSum));
Console.ReadKey();
}
#endregion
}
除了我想不通这部分: 如果有一个以上的答案,程序应该编写一个包含停车次数最少的一个手。
[你有什么试过](http://whathaveyoutried.com)? – Marco
是............. – ShaneKm
我想我应该先找到最大的数字...... – ShaneKm