繁体   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