一维数组的值代表了一个挡板的高度,宽度是数组序号的差,求这个一维数组与X轴围成的容器的最大面积。
转载请注明出处,联系我: t39q@163.com
本人热衷于数据库技术及算法的研究,志同道合之士, 欢迎探讨
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();