[Finding multiples of three from a list of integers]

Problem description: Given a list of integers from 1 to n, start counting from the first number. When encountering a multiple of 3, delete the number from the list until the end of the list.
Among the remaining numbers, starting from the first number, continue the previous count value, and when encountering a multiple of 3, delete the number.
Repeat the above steps until there is only one number left in the list.
Based on the specified number n, determine which number is left at the end.

Insert image description here
When I first saw the meaning of this question, I was confused. I didn’t know what the question meant. But I understood it after reading it a few times. The meaning of the question is to enter an n, and then in the 1-n integer list, start counting from the first number. For example, for n=5, it starts counting from 1. This count refers to the index of each position. When counting to 3, it is a multiple of three, so the value corresponding to the third number should be deleted. After deletion, the previous counting value starts counting from the first number. For example, the first step counts to 5, and the number 3 is After being deleted, counting starts from the first one again, and the counting value starts from 5! , so the index of the first number at this time is 6, which is a multiple of 3 and needs to be deleted. And so on…

Problem-solving steps:
1. Define a list to store n values, and create a temporary list B to store the index of the number to be deleted; 2.
Enter the loop, first determine whether the length of the original list is 1, and if it is 1, directly Output, otherwise, starting from the first number, define a counting variable. Each time a number is traversed, it is counted once. When the counting variable is a multiple of 3, the subscript of the element at this time is placed in B. When the original list is traversed, When , start traversing B from back to front, and then delete the corresponding element from the original list.
3. After traversing list B, delete the elements in B. Loop in this way until there is only one element left in the original list.

Code:

import java.util.*;
public class DeleteThreeTimes {
    
    
    public static void main( String[] args ) {
    
    
        //接收输入信息
        Scanner scanner = new Scanner(System.in);
        int nextInt = scanner.nextInt();
        //生成数组
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < nextInt; i++) {
    
    
            list.add(i+1);
        }

        //计数变量
        int count = 0;
        //每一次循环结束后,待删除的元素下标的集合
        ArrayList<Integer> removeIndex = new ArrayList<>();

        while (true){
    
    
            //记录当前集合长度
            int oldLength = list.size();
            if (oldLength == 1){
    
    
                System.out.println("最终结果:"+ list.toString());
                return;
            }
            //遍历集合
            for (int i = 0; i < oldLength; i++) {
    
    
                count++;
                if (count %3 == 0){
    
    
                    //记录待删除的元素下标
                    removeIndex.add(i);
                }
            }
            //循环结束,删除指定下标元素
            int size = removeIndex.size();
            //从后往前删,因为从前往后删,一但删除元素,就会导致后面未删除的元素下标变化
            for (int i = size; i >0; i--) {
    
    
                int x = removeIndex.get(i-1);
                list.remove(x);
            }
            //清空removeIndex
            removeIndex.clear();
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_45257495/article/details/132627223