- Public class Deleteeverytwo {
- public static void Main (string[] args) {
- int index = GETLASTDELETEDINDEX (8);
- System.out.println ("The Last index deleted is" + index);
- }
- /**
- *
- * @param a
- * Array length
- * @return The original subscript of the last deleted number
- */
- public static int getlastdeletedindex (int len) {
- If (len <= 0) { //return-1 if the array length does not meet the requirements
- return-1;
- }
- int[] arr = new Int[len];
- for (int i = 0; i < len; i++) { //Initialize the value of each element as the current subscript
- Arr[i] = len;
- }
- final int delflag = len + 1; //Remove flag bit
- int currentsize = len; //Record the current valid length of the array (that is, the number of elements that are not set to the delete flag), and finally to 0
- final int STEP = 2; //Step
- int count = 0; //Step count
- int lastdelindex = 0; //Record the subscript of the last deleted element
- int i = 0; //Cycle subscript
- While (currentsize! = 0) {
- if (arr[i]! = Delflag) { //interpret whether the current element equals the delete flag
- if (count++ = = Step) { //When the step count satisfies the step size
- Arr[i] = Delflag; //Set element to delete flag bit
- Lastdelindex = i; //Record the subscript of the place
- currentsize--; //valid array length minus 1
- Count = 0; //Step count zeroing
- System.out.println ("Deleted index is" + i% len);
- }
- }
- i = (i + 1)% Len; //subscript take -off to achieve cyclic subscript
- }
- return lastdelindex;
- }
- }
An array that deletes a number every two numbers, goes to the end of the array, and continues to go through the bad, resulting in the subscript of the last element removed