如何添加条件,以便我不进入循环内部。

huangapple go评论83阅读模式
英文:

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

问题

我正在处理一段代码,在这段代码中,你需要从数组'b'中分别减去数组'a'的对应元素,直到数组'a'中的所有元素都相等为止。条件是对于减法操作,必须满足a[i] > b[i]。但并不是每次都能使所有元素相等,所以在这种情况下,我希望我的代码打印出'-1',你可以无限制地进行多次减法操作。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        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
4 1 0 0

有效输入示例:

5
5 7 10 5 15
2 2 1 3 5

输出示例:

5 5 5 5 5
8
英文:

I am working on a code where you subtract respective elements of array 'a' from array'b' till you get all elements in array 'a' equal.<br />
Condition is that a[i]>b[i] for subtraction.<br />
But not every time getting all elements equal is possible, so in that case I would like my code to print '-1', how can I achieve it.<br /> I really tried hard to figure it out, don't give complex solutions as I am a beginner. you can subtract as many times you want.

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 &lt; n; i++) {// taking elements input
a[i] = sc.nextShort();
}
for (short i = 0; i &lt; n; i++) {// taking elements input
b[i] = sc.nextShort();
}
short minimumOfArraya = 0;
for (short i = 0; i &lt; n; i++) {// finding smallest element in array &#39;a&#39;
for (short j = 0; j &lt; n; j++) {
if (a[i] &lt; a[j]) {
minimumOfArraya = a[i];
}
}
}
boolean allequal = false;
int counter = 0;
while (!allequal) {
for (short i = 0; i &lt; n; i++) {// subtracting elements
if (a[i] == minimumOfArraya)
continue;
if (a[i] &gt;= b[i]) {
a[i] -= b[i];
counter++;
}
}
for (short i = 0; i &lt; n; i++) {
if (a[0] == a[i]) {
allequal = true;
} else {
allequal = false;
break;
}
}
}
for (int i = 0; i &lt; n; i++) {// printing array &#39;a&#39;
System.out.print(a[i] + &quot; &quot;);
}
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

答案1

得分: 0

快速找到最小值:

short minimumOfArraya = Short.MAX_VALUE;
for (short i = 0; i < n; i++) {// 在数组 '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++) {// 减去元素
        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 内部未增加 counter,代码可能会一直循环直到溢出。例如,如果最小值是 100,而 102 得到了 98。

英文:

The minimum can be found faster:

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

The for loop to check whether all are equal should initially have an allequal true,
and on finding a false, break. The initial setting to true was missing.

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

If counter was not increased inside the while, the code may very well stay looping till overflow. If the minimum was 100 and 102 got 98 for instance.

答案2

得分: 0

如果一些迭代产生的数字小于最小值,这清楚地表明您无法使所有元素相等。在减去后检查。

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

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

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

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

    // 循环的其余部分
    ...
}
英文:

If some of iterations producing number which is less than minimum, it clearly shows that you cannot make all elements equal. Check that after subtracting

while (!allequal) {
boolean impossible = false;
for (short i = 0; i &lt; n; i++) {
if (a[i] &lt; mimimumofArraya) {
impossible = true;
break;
}
if (a[i] == minimumOfArraya) continue;
if (a[i] &gt;= b[i]) {
a[i] -= b[i];
counter++;
}
}
if (impossible) {
counter = -1;
break;
}
// The rest of your loop
...
}

huangapple
  • 本文由 发表于 2020年10月27日 19:47:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64553807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定