zoukankan      html  css  js  c++  java
  • 201712-2 游戏 Java

    思路:
    第一感觉有点像约瑟夫环。想到用队列解决比较好理解

    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int k = sc.nextInt();
    		Queue<Integer> children = new LinkedList<Integer>();
    		for(int i=1;i<=n;i++) {
    			children.offer(i);
    		}
    		for(int j=1;children.size()!=1;j++) {
    			int temp = children.poll();//每个数都出队
    			if(!(j%k==0 || j%10==k)) {//如果不出局就再入队
    				children.offer(temp);
    			}
    		}
    		sc.close();
    		System.out.println(children.poll());
    	}
    
    }
    
    

    普通队列:
    LinkedList支持队列的行为,并且实现了Queue接口,上转型为Queue。
    add和offer都是将一个元素插入到队尾。offer方法不会返回null,add会返回null
    peak和element在不移除的情况下返回队头。peak会返回null
    poll和remove将移除并返回队头。poll会返回null

    此外还有优先级队列,双端队列可以学习,了解一下。

  • 相关阅读:
    P4213【模板】杜教筛
    【SDOI2006】线性方程组
    【AHOI2018】排列
    【NOI2001】炮兵阵地
    【NOIP2012】疫情控制
    【AHKOI2017】rexp
    【十二省联考2019】春节十二响
    【TJOI2014】匹配
    【AT2645】Exhausted?
    P3809 【模板】后缀排序
  • 原文地址:https://www.cnblogs.com/yu-jiawei/p/12370939.html
Copyright © 2011-2022 走看看