void MaxValue( int ai[], int n, int *value)
{
if (n <= 0 ) return ;
int i, j, k = 0 , temp = 0 ;
int number = ( int )n*(n- 1 )* 0.5 ;
int aNew[number];
// 求出所有的结果存放到 aNew[] 数组中
for (i = 0 ; i < n - 1 ; i++)
{
for (j = i + 1 ; j < n; j++)
{
if (ai[i] > ai[j])
{
aNew[k] = (j - i)*ai[j];
} else {
aNew[k] = (j - i)*ai[i];
}
k++;
}
}
// 用冒泡法把得到的所有结果排序
for (i = 0 ; i < number - 1 ; i++)
{
for (j = i + 1 ; j < number; j++)
{
if (aNew[i] > aNew[j])
{
temp = aNew[i];
aNew[i] = aNew[j];
aNew[j] = temp;
}
}
}
*value = aNew[number- 1 ];
}
int main ()
{
int a[ 4 ] = { 4 , 10 , 9 , 5 };
int myValue;
MaxValue(a, 4 , &myValue);
printf( "%d" , myValue);
return 0 ;
/*
测试的输出结果为:
i = 0, j = 1, j - i = 1, ai[i] = 4, ai[j] = 10, aNew[0] = 4
i = 0, j = 2, j - i = 2, ai[i] = 4, ai[j] = 9, aNew[1] = 8
i = 0, j = 3, j - i = 3, ai[i] = 4, ai[j] = 5, aNew[2] = 12
i = 1, j = 2, j - i = 1, ai[i] = 10, ai[j] = 9, aNew[3] = 9
i = 1, j = 3, j - i = 2, ai[i] = 10, ai[j] = 5, aNew[4] = 10
i = 2, j = 3, j - i = 1, ai[i] = 9, ai[j] = 5, aNew[5] = 5
排序后的数组为:
aNew[6] = {4, 5, 8, 9, 10, 12};
最后所要求的值为:
12
*/
package conmon.niuke;
public class VatProblem { public static void main(String[] args){ int[][] a={{2,5,7}, //key集合 {3,6,5}}; //value集合 System.out.println(getMaxVat(a)); } /**求出最大的桶 * @param a * @return */ public static int getMaxVat(int[][] a){ int max=count(a[0][0],a[1][0],a[0][1],a[1][1]); //循环求出最大的桶 for(int i=1;i<a[0].length-1;i++){ int temp=count(a[0][i],a[1][i],a[0][i+1],a[1][i+1]); max=max>temp?max:temp; } return max; } /**计算桶面积 * @param i * @param ai * @param j * @param aj * @return */ public static int count(int i,int ai,int j,int aj){ int height=ai,width=Math.abs(j-i); if(ai>aj){ height=aj; } return height*width; } }
#include <iostream> using namespace std; int maxArea(int a[], int size){ int i,j,lastj=size-1; int maxarea=0; for(i=0;i<size;++i){ for(j=size-1;j>i;--j){ if(lastj-a[i]>a[j]-a[i]) continue; int t_area=(j-i)*(a[j]-a[i]); if(t_area<0) t_area=-t_area; if(t_area>maxarea) maxarea = t_area; } } return maxarea; } int main(){ int a[]={1,2,3,4,5,6,7}; cout<<maxArea(a, sizeof(a)/sizeof(a[0])); return 0; }
int maxArea(int height[], int n) { int head = 0, tail = n - 1; int max = 0, temp = 0; while (head < tail) { temp = tail - head; if (height[head] < height[tail]) { temp *= height[head]; head++; } else { temp *= height[tail]; tail--; } if (temp > max) { max = temp; } } return max; }