2017-05-11 61 views
1

我有一个数组,它有大约1000个条目,每年分成12个条目,我试图找到每年的最大值,这样做我需要一次读取12个值并查找这12个值的最大值,然后继续处理阵列中的下一个12个值,依此类推,直到完成为止。一次查找数组12个元素的最大值

试图做到这一点,我做了一个临时数组来存储12个值,以及每年最高分数的最终数组。下面的代码不工作,我不确定为什么,我花了相当长的一段研究和不同的解决方案在尝试此,任何帮助将不胜感激:)

//double rain[]= new double [1268]; this is the array declared earlier with data in 

double maxRAINyear[]= new double [1200]; 
double temp [] = new double [12]; 
int arrayCounter = 0; 
int count = 0; 
for (int c = 36; c < rain.length; c++) { 
    temp[count] = rain[c]; 
    if (count == 12){ 
     Arrays.sort(temp); 
     double max = temp[temp.length - 1]; 
     maxRAINyear[arrayCounter] = max; 
     arrayCounter++; 
     count = 0; 
    } 
    count++; 

} 
+1

从什么时候开始C值为36? – Nemesis

+0

错误类型,它应该是12,36值是因为前36个值都是0,并且对于该程序的这部分不需要。 – Jamie

+4

它以什么方式不起作用?它会抛出异常还是产生不正确的输出? – Eran

回答

0

我在没有临时数组的情况下实现它,只是遍历数组。这应该更有效率。

int amountOfYears = rain.length/12-3; 
    double maxRAINyear[]= new double [amountOfYears]; 
    for(int i=0;i<amountOfYears;i++){ 

     //Find maximum for these 12 indixes 
     double max = Double.NEGATIVE_INFINITY; 
     for(int j=12*i+36;j<12*(i+1)+36;j++){ //use 12*i as an offset, that way, you don't need a temp array 
      if(rain[j] > max) 
       max = rain[j]; 
     } 
     //store maximum 
     maxRAINyear[i] = max; 

    } 

如果您还需要找到部分年,使用此

int amountOfYears = Math.ceil(rain.length/12f)-3; 
    double maxRAINyear[]= new double [amountOfYears]; 
    for(int i=0;i<amountOfYears;i++){ 

     //Find maximum for these 12 indixes 
     double max = Double.NEGATIVE_INFINITY; 
     int start = 12*i+36; 
     int end = Math.min(rain.length,12*(i+1)+36); 
     for(int j=start;j<end;j++){ //use 12*i as an offset, that way, you don't need a temp array 
      if(rain[j] > max) 
       max = rain[j]; 
     } 
     //store maximum 
     maxRAINyear[i] = max; 

    } 
+0

那个不知道的复杂的;)看到[gandaliter答案](http://stackoverflow.com/a/43915713/4391450) – AxelH

+0

这工作完美! :) – Jamie

+0

@Jamie执行过于复杂 – AxelH

2

这是很难看到有什么错你代码不知道它产生了什么,但无论如何,这不是一个很好的方法来做到这一点。

我假设你被限制为一个长数组的输入格式,即使多维数组可能会更有意义,这取决于它的用途。

// double rain[] = new double[1268]; // input 
double maxRAINyear[] = new double[(rain.length+11)/12]; 
double max = Double.NEGATIVE_INFINITY; 
for (int i = 0; i < rain.length; i++) 
{ 
    if (rain[i] > max) max = rain[i]; 
    if (i % 12 == 0) 
    { 
     maxRAINyear[i/12] = max; 
     max = Double.NEGATIVE_INFINITY; 
    } 
} 
if (rain.length % 12 != 0) maxRAINyear[maxRAINyear.length-1] = max; 

这会计算每个12位数字的最大值,而不是单独存储和排序它们。我假设有一整年存储。如果你想在最后考虑一个部分年份,这将需要修改。

+0

它确实结束于部分年份,在第5个月确切地说,我将如何解释这一点? – Jamie

+0

取决于你想用它做什么。因为我认为部分年份被忽略了。如果你想从中获得最大值,那么你需要在这种情况下将数组长度增加1,然后在循环结束后将max的值放入最后一个槽中。 – gandaliter

+0

你可以添加一个特殊的情况,其中如果(i == rain.length -1 && i%12!= 0)你做了maxRAINyear [i/12 + 1] = max –

0

count是11,你就递增到12和进入下一轮循环。现在temp[count] = rain[c];会尝试将值存储到temp的索引12中,但只有12个索引为0到11的条目。所以我怀疑您得到的是ArrayIndexOutOfBoundsException

我相信你应该在if声明前移动count++。这样,在造成任何破坏之前,12的计数将被重置为0。

0

如果数组长度不是范围的倍数,而没有进行多次检查,该解决方案的优点是可以工作。

public static double[] read(double[] array, int range) { 
    double[] result = new double[array.length/range + (array.length % range > 0 ? 1 : 0)]; //Just add one cell if the length is not a multiple of the range 


    double max = array[0]; 
    int i = 1; 
    while (i < array.length) { 
     if (i % range == 0) { //Next range 
      result[i/range - 1] = max; //Save last 
      max = array[i]; //Get current 
     } else if (array[i] > max) { 
      max = array[i]; 
     } 
     ++i; 
    } 
    result[result.length - 1] = max; //for the last range 
    return result; 
} 
相关问题