zoukankan      html  css  js  c++  java
  • 孩子们的游戏(圆圈中最后剩下的数)

    每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0...m-1报数....这样下去....直到剩下最后一个小朋友,可以不用表演,并且拿到牛客名贵的“名侦探柯南”典藏版(名额有限哦!!^_^)。请你试着想下,哪个小朋友会得到这份礼品呢?(注:小朋友的编号是从0到n-1)

    思路:使用数组模拟环

    public class Solution {
        public int LastRemaining_Solution(int n, int m) {
            if(n<1||m<1)return -1;
            int []a = new int[n];
            int i = -1,step=0,count=n;
            while(count>0){
                i++;
                if(i>=n) i =0;
                if(a[i] == -1)continue;
                step++;
                if(step==m){
                    a[i] = -1;
                    step=0;
                    count--;
                }
            }
            return i;
        }
    }
  • 相关阅读:
    Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并
    HDU 5787 K-wolf Number 数位DP
    python画图
    python文件操作
    python matplotlib绘图
    沟通
    ipython notebook的使用
    生活
    担心承担责任
    Large-scale Scene Understanding (LSUN)
  • 原文地址:https://www.cnblogs.com/LoganChen/p/6490585.html
Copyright © 2011-2022 走看看