CCF Java report game loop culling problem

【Problem Description】

  There are n children playing games in a circle, children are numbered from 1 to n, child number 2 sits clockwise from child number 1, child number 3 sits clockwise from child number 2, ..., child number 1 Sit in the clockwise direction of the nth child.
  At the beginning of the game, starting from the child No. 1, the number is reported clockwise, and the number reported by each child is the number reported by the previous child plus 1. If the number reported by a child is a multiple of k or its last digit (that is, the one digit of the number) is k, the child will be eliminated from the competition and will no longer participate in future reports. When there is only one child left in the game, that child wins.
  For example, when n=5, k=2:
  Child 1 counts as 1;
  Child 2 counts as 2 for elimination;
  Child 3 counts as 3;
  Child 4 counts as 4 for elimination;
  Child 5 counts as 5;
  1 Child number 6 will be eliminated; child number
  3 will be eliminated by number 7;
  child number 5 will be eliminated by number 8; child number
  3 will win.

  Given n and k, what is the number of the child who wins at the end?
Input format
  Enter a line, including two integers n and k, the meaning is as described in the title.
Output format
  Output one line, including an integer, indicating the number of the winning child.
Sample Input
5 2
Sample Output
3
Sample Input
7 3
Sample Output
4
Data Size and Conventions
  For all evaluation cases, 1 ≤ n ≤ 1000, 1 ≤ k ≤ 9.


popular ideas

  1. A simple cycle, the original element does not move, and it goes round and round.
  2. Use an array to store n elements, representing n children
  3. After elimination, it will no longer participate in the report, but it is still in the array, so it needs to be marked. The initial value of the array is 0, which means that it is not eliminated, and it is set to 1 when it is eliminated.
  4. Use the cursor p to perform a circular traversal one by one to find the next child to report the number.
  5. When the number of eliminations is n-1, the algorithm ends;

The implementation function is as follows

import java.util.Scanner;
public class Main {
    private static int lastone(int n ,int k) {
        if(n==1) return 1;
        if(k==1) return n;

        //创建一个以为数组,ch表示所有朋友,初始值都为0,表示未淘汰。
        int []ch=new int[n+1];

        int out=0;//淘汰人数
        int p=1;//游标 找到下一个报数的下标。

        for(int i=2;out<n;i++)//i为当前报数值
        {
            //找到下一个报数下标p
            for(p=p+1;;p++)
            {
                if(p>n)      p=p-n;//循环重新开始
                if(ch[p]==0) break;
            }

            //ch[p] 报数为i,判断是否满足条件,
            if(i%k==0||i%10==k)
            {
                ch[p]=1;//剔除淘汰掉
                out++;//淘汰人数+1
            }
        }
        return p;
    }

    public static void main(String[] args) {
        Scanner oin=new Scanner(System.in);
        int n=oin.nextInt();//n个人
        int k=oin.nextInt();//报的数为k的倍数或其末位数为k的被淘汰
        System.out.println(lastone(n, k));
    }

}

Copy's awesome idea#### (some flaws, can't judge the case where the single digit value is k)

The meaning of the title can be understood by another model:
there is a group of passengers waiting for the boat at the ferry, and they stand in a circle according to the circle. Now there is only one small boat, and the master can only carry one person at a time. The master takes the passenger MM next to him as the starting point 1, and then says to everyone: "One by one, move slowly in a circle, I will start counting from this MM, move from my left to the right, and I will count one for every digit I move. When you count to k, you pause first, and I will send the one that counts to k to the other side first. Then continue ".
write picture description here

  1. The original element constantly moves the position, the title is clockwise, and the position is counterclockwise.
  2. After elimination, the element is not stored, so the element is constantly decreasing.
  3. The question says that the reported number is a multiple of k, that is, k distances. We move k-1 person to the right side of the boat, then the kth person is the closest to the boat and is selected.
  4. Renumber and repeat until only one person remains.

Renumbering, if you use an array, it is very troublesome, it is best to use a data structure that can be deleted at the head and added at the end, so choose - linked list List

The implementation function is as follows

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
    private static int  lastone2(int  n,int k) {
        List<Integer> list=new ArrayList<Integer>(n);
        for(int i=0;i<n;i++)
            list.add(i+1);

         while(list.size()>1)
         {
             for(int i=0;i<k-1;i++)
             {
                 list.add(list.remove(0));//头元素挪动到结尾处

             }
             list.remove(0);
         }
        return list.get(0);
    }

    public static void main(String[] args) {
        Scanner oin=new Scanner(System.in);
        int n=oin.nextInt();//n个人
        int k=oin.nextInt();//报的数为k的倍数的被淘汰
        System.out.println(lastone2(n, k));
    }


}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325423043&siteId=291194637