递归分治

时间:2020-03-08 11:07:20

标签: java

Sample with input and outputs我正在尝试对整数数组应用分而治之定理,以便在其中找到最差情况的索引。在初始数组上,我必须在中点两侧将其拆分为两个。重复执行此操作,并在每个实例中删除中点。我要检查每个子阵列上的峰值,但最后我只打印所有最坏情况的索引。输入in将是任何给定实例处数组的初始长度。到目前为止,我已经能够请求和输入并打印出给定长度n的索引数组。鉴于我对Java不太熟悉,但我不知道该怎么做。

class Peak {


    public static void main(String a[]){
         List<Integer> al = new ArrayList<Integer>();

            Scanner s = new Scanner(System.in); 
            System.out.println("enter the length of the array: ");
            int n = s.nextInt();
            for (int i = 0; i < n ; i++) 
            al.add(i);
            System.out.println(al);
            int size = al.size();

            if (al.size()%2==0) {
                 //Sublist to ArrayList


                 ArrayList<Integer> lhs = new ArrayList<Integer>(al.subList(0, (size  - 1)/2));
                 ArrayList<Integer> rhs = new ArrayList<>(al.subList((size + 1)/2, n));

            }

            if (al.size()%2==1) {
                 //Sublist to ArrayList
                 ArrayList<Integer> lhs = new ArrayList<Integer>(al.subList(0, (size  - 1)/2));
                 ArrayList<Integer> rhs = new ArrayList<>(al.subList((size + 1)/2, n));

            }
      }

0 个答案:

没有答案