如果数组中的下一个元素在增加,如何检查数组中的每个变量?

我应该编写一个程序来读取一个整数数组并输出数组中“三元组”的数量。


“三元组”是相差 1 的递增顺序的三个连续整数(即 3,4,5 是三元组,但 5,4,3 和 2,4,6 不是)。


如何检查“三元组”?


当前代码:


import java.util.Scanner;


class Main {

    public static void main(String[] args) {

        // put your code here

        Scanner scanner = new Scanner(System.in);

        int size = scanner.nextInt();

        int[] array = new int[size];

        int iterator = 0;

        for(int i = 0; i < size; i++){

            array[i] = scanner.nextInt();

        } for(int j=0; j < size; j++){

            iterator++;

        }

    }

}


偶然的你
浏览 159回答 4
4回答

慕仙森

以下代码遍历整个整数数组。在循环内部,检查数组 ( (i + 2) < array.Length) 中是否存在第三个整数,其他 2 个条件都是关于 value1 是否与 value2 相同减 1 (array[i] == array[i + 1] - 1和array[i + 1] == array[i + 2] - 1):for (int i = 0; i < array.Length; i++){&nbsp; &nbsp; if((i + 2) < array.Length && array[i] == array[i + 1] - 1 && array[i + 1] == array[i + 2] - 1)&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Three values at indexes" + i + " " + (i + 1) + " and " + (i + 2) + " are a triple");}下面的代码是 C# 的,遗憾的是与 Java 不那么容易兼容,我将把它留在这里给任何想知道它在 C# 中如何处理的人(变量是vt所谓的ValueTriple):(int, int, int) vt;for (var i = 0; i < array.Length; i++){&nbsp; &nbsp; if (i + 2 >= array.Length) continue;&nbsp; &nbsp; vt = (array[i], array[i + 1], array[i + 2]);&nbsp; &nbsp; if (vt.Item1 == vt.Item2 - 1 && vt.Item2 == vt.Item3 - 1)&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"Three values at indexes {i}, {i + 1} and {i + 2} (Values: {array[i]}, {array[i + 1]}, {array[i + 2]}) are a triple");}

至尊宝的传说

您可以尝试以下代码import java.util.Scanner;public class Triplet {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; // put your code here&nbsp; &nbsp; &nbsp; &nbsp; Scanner scanner = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; int size = scanner.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; int[] array = new int[size];&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < size; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[i] = scanner.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Integer counter = 0;&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < size-2; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(array[i] == array[i+1] - 1 && array[i] == array[i+2] - 2) { //checking if three consecutive ints in increasing order differing by 1&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(counter);&nbsp; &nbsp; }}希望这会有所帮助。

神不在的星期二

找出三胞胎数量的方法可能如下所示。然后,您只需根据输入的获取方式和您希望显示的结果来调用该方法。public static int getNumberOfTriplets(int[] toBeChecked) {&nbsp; &nbsp; int numberOfTriplets = 0;&nbsp; &nbsp; int nextIndex = 0;&nbsp; &nbsp; while (nextIndex < toBeChecked.length - 2) {&nbsp; &nbsp; &nbsp; &nbsp; int first = toBeChecked[nextIndex];&nbsp; &nbsp; &nbsp; &nbsp; int second = toBeChecked[nextIndex + 1];&nbsp; &nbsp; &nbsp; &nbsp; int third = toBeChecked[nextIndex + 2];&nbsp; &nbsp; &nbsp; &nbsp; if ((first + 1 == second) && (second + 1 == third)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numberOfTriplets++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; nextIndex++;&nbsp; &nbsp; }&nbsp; &nbsp; return numberOfTriplets;}

白衣非少年

不管允许数字超过一个三元组,答案与我个人的处理方式非常相似://determines if the input sequence is consecutivepublic boolean isConsecutive(int... values) {&nbsp; &nbsp; return IntStream.range(1, values.length)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .allMatch(i -> values[i] == values[i - 1] + 1);}public int countTriples(int[] input, boolean uniques) {&nbsp; &nbsp; if (input.length < 3) {&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; }&nbsp; &nbsp; int back = 0;&nbsp; &nbsp; for(int i = 2; i < input.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (isConsecutive(input[i - 2], input[i - 1], input [i]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; back++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (uniques) { //whether to disallow overlapping numbers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i += 2; //triple found, ignore the used numbers if needed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return back;}然后调用它:Int[] input = new int[] {1, 2, 3, 5, 6, 7, 8};countTriples(input, true); //3countTriples(input, false); //2
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java