简体   繁体   English

在数组中搜索一个递增的整数序列

[英]Searching an array for a sequence of increasing integers

Here's the task:这是任务:

Create a program that looks through a 200 -element array of randomly-generated integers with values between 25 and 225 .创建一个程序,该程序查看200元素的随机生成整数数组,其值介于25225之间。 The program should look for n consecutive indices that hold non-decreasing values - for example:程序应该寻找n个连续的包含非递减值的索引 - 例如:

28, 57, 88, 153, 201 is considered a success 28, 57, 88, 153, 201被认为是成功的

28, 57, 32, 153, 201 is considered a failure. 28, 57, 32, 153, 201被视为失败。

Output should look something like:输出应该类似于:

An increasing consecutive run of 5 elements found at:

Index 41 contains 28
Index 42 contains 57
Index 43 contains 88
Index 44 contains 153
Index 45 contains 201

There are many ways to go about this task.有很多方法可以完成这项任务。 I've included a working step-by-step solution below.我在下面提供了一个工作分步解决方案。

SOLUTION解决方案

First, apply the given information (lower bound, upper bound, array size).首先,应用给定的信息(下限、上限、数组大小)。

    int min = 25;
    int max = 225;
    int set[] = new int[200];

Set n to an arbitrary value.n设置为任意值。 I will use 5 for demonstration purposes.我将使用 5 进行演示。

    int n = 5;

Create a counter variable to record the number of consecutive integers found.创建一个counter变量来记录找到的连续整数的数量。 Further on we will add logic to reset the counter every time a sequence of numbers does not work.进一步,我们将添加逻辑以在每次数字序列不起作用时重置计数器。

    int counter = 0;

Start iterating.开始迭代。 Create a for loop to fill the array with '200' elements between the specified bounds.创建一个for循环,用指定边界之间的“200”个元素填充数组。

    for(int i = 0; i < set.length; i++){
        set[i] = (int)(Math.random() * (max - min + 1) + min);

Print out the contents of every index.打印出每个索引的内容。

        System.out.println((i + 1) +") " + "Index " + i + " contains " + set[i]);

The first if statement: this will increment 'counter' by 1 every time a sequence fits the pattern, but resets it back to 0 when the comparison fails.第一个if语句:每次序列符合模式时,这会将 'counter' 增加 1,但在比较失败时将其重置为 0。

   if(i > 0){   
        if(set[i] >= set[i-1]){counter++;}
        else{counter = 0;}  
    }// End if

Now create a while statement that activates when the value of counter becomes equal to the value of n .现在创建一个 while 语句,当counter的值等于n的值时激活该语句。 This will print out the statement An increasing consecutive run of 5 elements found at: and then lead to a for loop.这将打印出语句An increasing consecutive run of 5 elements found at:然后导致 for 循环。

    while(counter == n){

           System.out.println("An increasing consecutive run of " + n + " elements found at:");

Now set up this loop to simply print out the n lines that were successful.现在设置这个循环来简单地打印出成功的n行。

    for(int j = i - n; j < i; j++){
        System.out.println("Index " + j + " contains " + set[j]);
    } //End for

Tell the code to stop running once all conditions are met.一旦满足所有条件,告诉代码停止运行。

       return;

Just close the while and for loops and the program is now complete.只需关闭 while 和 for 循环,程序就完成了。

        } //End while

    } //End for

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

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