簡體   English   中英

如何添加條件,這樣我就不會在循環中使用 go

[英]How to add a condition so I don't go inside the loop

我正在編寫一個代碼,在該代碼中,您從數組“b”中減去數組“a”的各個元素,直到數組“a”中的所有元素都相等。
條件是 a[i]>b[i] 為減法。
但並非每次都使所有元素都相等是可能的,所以在那種情況下我希望我的代碼打印“-1”,我該如何實現它。
我真的很努力想弄明白,因為我是初學者,所以不要給出復雜的解決方案。 您可以減去任意多次。

Scanner sc = new Scanner(System.in);
    short n = sc.nextShort();
    short a[] = new short[n];
    short b[] = new short[n];
    for (short i = 0; i < n; i++) {// taking elements input
        a[i] = sc.nextShort();
    }
    for (short i = 0; i < n; i++) {// taking elements input
        b[i] = sc.nextShort();
    }
    short minimumOfArraya = 0;
    for (short i = 0; i < n; i++) {// finding smallest element in array 'a'
        for (short j = 0; j < n; j++) {
            if (a[i] < a[j]) {
                minimumOfArraya = a[i];
            }
        }
    }

    boolean allequal = false;
    int counter = 0;
    
    while (!allequal) {
        for (short i = 0; i < n; i++) {// subtracting elements
            if (a[i] == minimumOfArraya)
                continue;
            if (a[i] >= b[i]) {
                a[i] -= b[i];
                counter++;
            }

        }
        for (short i = 0; i < n; i++) {
            if (a[0] == a[i]) {
                allequal = true;
            } else {
                allequal = false;
                break;
            }

        }
    }
    for (int i = 0; i < n; i++) {// printing array 'a'
        System.out.print(a[i] + " ");
    }
    System.out.println();
    System.out.println(counter);


4
5 7 4 3//infinite loop
4 1 0 0

working input
5
5 7 10 5 15
2 2 1 3 5
output
5 5 5 5 5 
8

可以更快地找到最小值:

short minimumOfArraya = Short.MAX_VALUE;
for (short i = 0; i < n; i++) {// finding smallest element in array 'a'
    if (a[i] < minimumOfArraya]) {
        minimumOfArraya = a[i];
    }
}

用於檢查是否全部相等的 for 循環最初應該有一個 allequal true,並在發現 false 時中斷。 缺少初始設置為 true。

boolean allequal = false;
int counter = 0;

while (!allequal) {
    for (short i = 0; i < n; i++) {// subtracting elements
        if (a[i] == minimumOfArraya)
            continue;
        if (a[i] >= b[i]) {
            a[i] -= b[i];
            counter++;
        }
    }
    allequal = true;
    for (short i = 0; i < n; i++) {
        allequal = a[0] == a[i];
        if (!allequal) {
            break;
        }
    }
}

如果計數器在 while 內沒有增加,代碼很可能會一直循環直到溢出。 例如,如果最小值為 100,則 102 得到 98。

如果某些迭代產生的數字小於最小值,則清楚地表明您無法使所有元素相等。 檢查減去后

while (!allequal) {
    boolean impossible = false;
    for (short i = 0; i < n; i++) {
        if (a[i] < mimimumofArraya) {
            impossible = true;
            break;
        }

        if (a[i] == minimumOfArraya) continue;

        if (a[i] >= b[i]) {
            a[i] -= b[i];
            counter++;
        }
    }

    if (impossible) {
        counter = -1;
        break;
    }

    // The rest of your loop
    ...
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM