一維數組的值代表了一個擋板的高度,寬度是數組序號的差,求這個一維數組與X軸圍成的容器的最大面積。
轉載請註明出處,聯繫我: [email protected]
本人熱衷於數據庫技術及算法的研究,志同道合之士, 歡迎探討
int[] a = new int[] { 1, 8, 6, 2, 5, 4, 8, 3, 7,8,1,4,3 };
Random r1 = new Random(365);
Random r2 = new Random(1234);
int n1, n2,area=0,tempArea=0,tempN1=0,tempN2=0;
for (int i = 0; i < 10000; i++)
{
n1 = r1.Next(0, a.Count() - 1);
n2 = r2.Next(n1 + 1, a.Count()-1+1);
if (a[n1] <= a[n2])
{
tempArea = a[n1] * (n2 - n1);
if (tempArea > area)
{
area = tempArea;
tempN1 = n1;
tempN2 = n2;
}
}
else
{
tempArea = a[n2] * (n2 - n1);
if (tempArea > area)
{
area = tempArea;
tempN1 = n1;
tempN2 = n2;
}
}
}
textBox1.Text = area.ToString();
textBox2.Text = tempN1.ToString();
textBox3.Text = tempN2.ToString();