简体   繁体   中英

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

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.
Condition is that a[i]>b[i] for subtraction.
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.
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 < 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

The minimum can be found faster:

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

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

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.

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 < 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
    ...
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM