There is the following cycle:

while (b > 0){ c = a % b; massM[i] = c; a = b; massA[i] = a; System.out.print(massA[i] + " "); b = c; } 

When entering a = 525, b = 231; The massA [i] array has the form {231, 63, 42, 21} how to make it so that the array would be {525, 231, 63, 42, 21}, that is, entered and become at the beginning of the array.

    2 answers 2

    Code:

     int a = 525; int b = 231; ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add(a); arr.add(b); while (arr.get(arr.size() - 1) != 0) { arr.add(arr.get(arr.size() - 2) % arr.get(arr.size() - 1)); } for (int i = 0; i < arr.size(); i++) { System.out.println(arr.get(i)); } 

    Conclusion:

    525 231 63 42 21 0


    If zero is not needed at the end, then there are the following options:

    1. Delete the last element at the end of the while .
    2. Do not display the last item.
    3. Add a check inside a while :

       int c; while (arr.get(arr.size() - 1) != 0) { c= arr.get(arr.size() - 2) % arr.get(arr.size() - 1); if (c!=0) arr.add(c); } 

      swap lines a = b; and massA[i];

      • In this case, the last element of the array disappears: {525, 231, 63, 42} - Binary
      • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky