zoukankan      html  css  js  c++  java
  • sicily 猴子选大王

    题目描述

    猴子选大王,有N只猴子,从1~N进行编号。它们按照编号的顺时针方向,排成一个圆圈,然后从第一只猴子开始报数。第一只猴子报1,以后每只猴子报的数字都是它前面猴子所报数字加1。如果一只猴子报的数字是M,则该猴子出列,下一只猴子重新从1开始报数。剩下的猴子继续排成一个圆圈报数,直到全部的猴子都出列为止。最后一个出列的猴子胜出。
     

    输入格式

    The first line is an integer t, indicating the number of test cases. Then there are t lines and each line contains two positive integer N(0<N<=100) and M(0<M<=100).

    输出格式

    For each test case, print out the number of the Monkey King.

    样例输入
     将样例输入复制到剪贴板
    2
    5 2
    4 3
    
    样例输出
    3
    1
    --------------------------我不是分割线--------------------------------------------------------------

    分析:开始时并没有考虑太多,就选择了用struct的数组来简单实现,不过时间上耗费略大
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    
    struct data
    {
        int count;
        bool is;
    };
    
    int cmp(data a[],int n){
        int count1=0,out;
        for (int i = 0; i < n; ++i)
        {
            if (a[i].is==0)
                {
                    ++count1;
                    out=a[i].count;
                }    
        }
        if (count1==1)
        {
            return out;
        }
        else{
            return 0;
        }
    }
    int main(int argc, char const *argv[])
    {
        int t;
        cin>>t;
        for (int i = 0; i < t; ++i){
            int m,n;
            cin>>m>>n;
            data monkey[m];
            for (int i = 0; i <m; ++i)
            {
                monkey[i].count=i+1;
                monkey[i].is=0;
            }
            int ji=0;int count2=0;int i=0;
            int isf=cmp(monkey,m);
            while(!isf){
                    if (monkey[i].is==0)
                    {
                        ++count2;
                        if (count2==n)
                        {
                            monkey[i].is=1;
                            count2=0;
                        }
                    }
                    ++i;
                    if(i==m){
                        i=0;/*实现循环,保证由count2来决定循环撤出的有效性*/
                    }
                    isf=cmp(monkey,m);
            }
            cout<<isf<<endl;
        }
        return 0;
    }


  • 相关阅读:
    用js onselectstart事件鼠标禁止选中文字
    模仿苹果菜单的导航
    返回页面顶部
    商品展示的放大镜效果
    键盘控制Div的移动
    Div跟随鼠标移动
    瀑布流的布局(功能还没有完善)
    类似时光轴的效果
    ie6-ie8中不支持opacity透明度的解决方法
    :active pseudo-class doesn't work in mobile safari
  • 原文地址:https://www.cnblogs.com/liugl7/p/4193310.html
Copyright © 2011-2022 走看看