簡體   English   中英

刪除數組的相鄰元素

[英]Deleting the neighbouring elements of an array

假設我有一個數組A。僅當第ith個元素同時具有第(i-1)個和第(i + 1)個相鄰元素並且縮小數組的大小時,才需要刪除所有的第ith個元素。以及何時找到該元素。 另外,如果任何這樣的元素都滿足此條件,則還可以根據以下公式每次計算其成本:

成本=(A [i] * A [i-1])+(A [i] * A [i + 1])+(A [i-1] * A [i + 1]); 例:

A = {1,2,3,4}

刪除元素“ 2”后:A = {1,3,4}

刪除元素“ 3”后:A = {1,4}

我不知道如何使用ArrayList。 有人可以指導我,僅通過使用數組概念來完成此任務,因為我無法繼續解決此問題?

PS:不是功課

這是我的代碼:

import java.util.*;


 class TestClass
{
    public static void main(String args[] ) throws Exception
{

    Scanner sc = new Scanner(System.in);
    int T = sc.nextInt();
    int cost=0;
    for (int i = 0; i < T; i++)   // scanning no. of test cases
     {
       int N = sc.nextInt();  // scanning no. of elements
        int[] A = new int[N];
        for(int j=0 ; j<N ; j++)
        {
          A[j] = sc.nextInt();
        }

        while (A.length>2)
        {
              cost = cost + getResultForLocation(A, 1);
              A = reduceArray(A, 1);
          }
    }

    System.out.println(cost);
}

static int getResultForLocation(int[] array, int location)
{
int sum = 0;

sum = sum + (array[location] * array[location - 1]) + (array[location] * array[location + 1]) + (array[location - 1] * array[location + 1]);

    return sum;
}


static int[] reduceArray (int[] array, int locationToRemove)
 {
if (array==null || array.length<=2)
 {
  return array;
}
if (locationToRemove == array.length || locationToRemove==1)
 {
  return array;
}
int[] returnArray = new int[array.length-1];
for (int i=0;i<locationToRemove;i++)
{
  returnArray[i]=array[i];
}
for (int i=locationToRemove;i<array.length-1;i++)
 {
  returnArray[i]=array[i+1];
}
return returnArray;
}


 }

您是否必須使用Arraylist? 在這種情況下,請嘗試此 那很好地解釋了Arraylist的工作方式。

看一下這些方法(在您的代碼中工作):

import java.util.*;


 class TestClass
{
public static void main(String args[]) throws Exception {

    Scanner sc = new Scanner(System.in);
    int numberOfTests = sc.nextInt();
    int cost = 0;
    for (int i = 0; i < numberOfTests; i++) // scanning no. of test cases
    {
      int arraySize = sc.nextInt(); // scanning no. of elements
      int[] array = new int[arraySize];
      for (int j = 0; j < arraySize; j++) {
        array[j] = sc.nextInt();
      }

      while (array.length > 2) {
        cost = cost + getResultForLocation(array, 1);
        array = reduceArray(array, 1);
      }
    }

    System.out.println(cost);
  }

  static int getResultForLocation(int[] array, int location) {

    int cost = 0;
    if (location > 0 && location < array.length - 1) {
      // logic part
      cost = cost + (array[location] * array[location - 1]) + (array[location] * array[location + 1]) + (array[location - 1] * array[location + 1]);
    }
    return cost;
  }

  static int[] reduceArray(int[] array, int locationToRemove) {

    if (array == null || array.length <= 2) {
      return array;
    }
    if (locationToRemove == array.length) {
      return array;
    }
    int[] returnArray = new int[array.length - 1];
    for (int i = 0; i < locationToRemove; i++) {
      returnArray[i] = array[i];
    }
    for (int i = locationToRemove; i < array.length - 1; i++) {
      returnArray[i] = array[i + 1];
    }
    return returnArray;

}

方法getResultForLocation計算位置x中元素的成本(如果它是合法位置),然后將其返回。

reduceArray方法刪除位置x的元素,並返回一個較小的數組。

在主要部分中,我建立了一個累加可能性-從第一個合格的位置開始,計算成本,刪除物料-重復進行直到陣列減少為兩個成員,然后在每個步驟累加成本。 此處顯示的數組的結果為30。

在控制台中輸入數字:1 4 1 2 3 4

結果是30

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM