【编程题解】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!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值