我有以下的解決,我不知道如何處理這樣的:找到最好的情況下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