亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果數組中的下一個元素在增加,如何檢查數組中的每個變量?

如果數組中的下一個元素在增加,如何檢查數組中的每個變量?

偶然的你 2023-06-04 10:27:25
我應該編寫一個程序來讀取一個整數數組并輸出數組中“三元組”的數量?!叭M”是相差 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++;        }    }}
查看完整描述

4 回答

?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

以下代碼遍歷整個整數數組。在循環內部,檢查數組 ( (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++)

{

    if((i + 2) < array.Length && array[i] == array[i + 1] - 1 && array[i + 1] == array[i + 2] - 1)

        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++)

{

    if (i + 2 >= array.Length) continue;

    vt = (array[i], array[i + 1], array[i + 2]);

    if (vt.Item1 == vt.Item2 - 1 && vt.Item2 == vt.Item3 - 1)

        Console.WriteLine($"Three values at indexes {i}, {i + 1} and {i + 2} (Values: {array[i]}, {array[i + 1]}, {array[i + 2]}) are a triple");

}


查看完整回答
反對 回復 2023-06-04
?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

您可以嘗試以下代碼


import java.util.Scanner;


public class Triplet {


    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];

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

            array[i] = scanner.nextInt();

        }

        Integer counter = 0;

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

            if(array[i] == array[i+1] - 1 && array[i] == array[i+2] - 2) { //checking if three consecutive ints in increasing order differing by 1 

                counter++;

            }

        }

        System.out.println(counter);

    }


}

希望這會有所幫助。


查看完整回答
反對 回復 2023-06-04
?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

找出三胞胎數量的方法可能如下所示。然后,您只需根據輸入的獲取方式和您希望顯示的結果來調用該方法。


public static int getNumberOfTriplets(int[] toBeChecked) {

    int numberOfTriplets = 0;

    int nextIndex = 0;

    while (nextIndex < toBeChecked.length - 2) {

        int first = toBeChecked[nextIndex];

        int second = toBeChecked[nextIndex + 1];

        int third = toBeChecked[nextIndex + 2];

        if ((first + 1 == second) && (second + 1 == third)) {

            numberOfTriplets++;

        }

        nextIndex++;

    }

    return numberOfTriplets;

}


查看完整回答
反對 回復 2023-06-04
?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

不管允許數字超過一個三元組,答案與我個人的處理方式非常相似:


//determines if the input sequence is consecutive

public boolean isConsecutive(int... values) {

    return IntStream.range(1, values.length)

            .allMatch(i -> values[i] == values[i - 1] + 1);

}


public int countTriples(int[] input, boolean uniques) {

    if (input.length < 3) {

        return 0;

    }

    int back = 0;

    for(int i = 2; i < input.length; i++) {

        if (isConsecutive(input[i - 2], input[i - 1], input [i]) {

            back++;

            if (uniques) { //whether to disallow overlapping numbers

                i += 2; //triple found, ignore the used numbers if needed

            }

        }

    }

    return back;

}

然后調用它:


Int[] input = new int[] {1, 2, 3, 5, 6, 7, 8};

countTriples(input, true); //3

countTriples(input, false); //2


查看完整回答
反對 回復 2023-06-04
  • 4 回答
  • 0 關注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號