简体   繁体   English

Java数组和子数组

[英]Java Array and sub-array

In the picture I created an array that I gave 15 as an argument. 在图片中,我创建了一个数组,并给出了15作为参数。 I have inserted random integers between 0 and 5 (inclusive) into this directory. 我已在此目录中插入0到5(含)之间的随机整数。

1) number of plains, 2) the widest width of the flat and 3) this widest plane is the starting point in the array 1)平原数量,2)平面最宽的宽度,以及3)这个最宽的平面是阵列中的起点

How can I write a screen-displaying program in Java programming language? 如何用Java编程语言编写屏幕显示程序?

(For example, the array is 1 0 5 5 5 2 2 3 3 4 4 4 4 0 1. The number of levels is 8. That is, by taking the same number of repetitions of 1, the array becomes a sub-array which is the same as all components. The largest plain 4 4 4 4 is the largest level number 4. The largest level is the first 4, 9th elements in the array.) (例如,数组为1 0 5 5 5 2 2 3 3 4 4 4 4 0 01。级别数为8。即,通过重复相同的重复数1,该数组成为子数组。这与所有组件相同。最大的普通4 4 4 4是最大的层数4。最大的层是数组中的前4个,第9个元素。)

Please help me. 请帮我。

Code: 码:

public class Zehra {  
    public static void main(String args[]) {
        int N = Integer.parseInt(args[0]);  
        int[] x = new int[N];  

        for(int i=0; i<N; i++)  
            x[i] = (int)(Math.random()*6);  
        for(int i=0; i<N; i++)  
            System.out.print(x[i] + " ");  

        System.out.println(" ");  
    }  
}

在此处输入图片说明

Don't expect anyone around here to write the code for you. 不要期望周围的任何人为您编写代码。 Rather I'd try to guide you in the right direction. 相反,我会尝试引导您朝着正确的方向发展。 Iterate once through the array, all the time keeping track of: 遍历数组一次,始终跟踪:

  • The start index of the current run of equal elements 当前运行的相等元素的起始索引
  • The value of the current run 当前运行的值
  • The number of runs encountered 遇到的运行次数
  • The start index and length of the longest run of equal elements before the current. 当前元素之前相等元素的最长游历的起始索引和长度。

Whenever you encounter a number that is not equal to the value in the current run, you know that a new run begins. 每当遇到与当前运行中的值相等的数字时,您就会知道新的运行开始。 At this time (a) add one to the count of runs (b) see if the run that is ending is longer than the previous longest run, and if so, record it as the new longest run, both its start index and its length. 此时(a)在运行次数中添加一个(b)查看结束的运行是否长于先前的最长​​运行,如果是,则将其记录为新的最长运行,同时记录其起始索引和长度。 After the loop ends, remember to count the last run and see if it is longer than any other run. 循环结束后,请记住计算最后一次运行的时间,看它是否比其他任何运行时间都长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM