【編程題解】Game of Throne

Description

After the death of DaenerysStormborn of the House Targaryen, the First of Her Name, the Unburnt, Queen of Meereen, Queen of the Andals and the Rhoynar and the First Men, Khaleesi of theGreat Grass Sea, Breaker of Chains and Mother of Dragons, several leaders decided to play a game to decide who would sit on the Iron Throne,There are n people in acircle, and their ids are 1 ~ n (n is close to 1).

 

Now counting starts from number1, the person who counted to m in the first round will be out, and the second round will start counting from the next person and the person who countedto m^2 will be out.

And so on, until the person who counted to m^(n−1)in the last round is out, leaving the last one.

Output the number of this person.

 

Input

One line, containing two integersn and m.

Data range:n≤15, m≤5n≤15, m≤5

 

Output

Output the number of the lastperson who is left.

Sample Input 1 

5 2

Sample Output 1

5

 

Personal Answer  (using language:JAVA)  Not necessarily right

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();

        int [] array = new int[n];
        for(int i=0;i<n;i++){
            array[i]=i+1;
        }

        List<Integer> list = Arrays.stream(array).boxed().collect(Collectors.toList());
        LinkedList linklist=new LinkedList(list);

        int delete_num = m;
        int times=0;
        int start_num=0;
        while ((linklist.size()!=1)){
            times++;
            delete_num=(int)Math.pow(m,times);
            int tmp=(start_num+delete_num-1)%linklist.size();
            linklist.remove(tmp);
            start_num=tmp;
        }

        System.out.println(linklist.get(0));

    }
}

Welcome to communicate!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章