zoukankan      html  css  js  c++  java
  • hdu 4841 圆桌问题(用vector模拟约瑟夫环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4841


    圆桌问题

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 104    Accepted Submission(s): 17


    Problem Description
    圆桌上围坐着2n个人。当中n个人是好人。另外n个人是坏人。假设从第一个人開始数数,数到第m个人。则马上处死该人。然后从被处死的人之后開始数数,再将数到的第m个人处死……依此方法不断处死围坐在圆桌上的人。

    试问预先应怎样安排这些好人与坏人的座位,能使得在处死n个人之后。圆桌上围坐的剩余的n个人全是好人。

     
    Input
    多组数据,每组数据输入:好人和坏人的人数n(<=32767)、步长m(<=32767)。
     
    Output
    对于每一组数据,输出2n个大写字母,‘G’表示好人,‘B’表示坏人,50个字母为一行。不同意出现空白字符。

    相邻数据间留有一空行。

     
    Sample Input
    2 3 2 4
     
    Sample Output
    GBBG BGGB
     
    Source
     
    Recommend
    We have carefully selected several similar problems for you:  4896 4895 4894 4892 4890 

    代码例如以下:

    //用vector模拟约瑟夫环
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int flag[50017];
    vector<int> v;
    int main()
    {
        int n, m;
    	int tot, now;
        int i;
        while(~scanf("%d%d",&n,&m))
        {
            v.clear();
            tot=2*n;
            for(i = 1; i <= tot; i++)
            {
                v.push_back(i);
                flag[i]=0;
            }
            now=1; 
            /*for(i = 0; i < v.size(); i++)
            {
            printf("%d:%d
    ",i,v[i]);
            }*/
            while( tot > n )//仅仅寻找坏人
            {
                now+=(m-1);
                if(now <= tot)
                {
                    flag[v[now-1]]=1;//从0開始计算
                    //printf(">%d<
    ",v[now-1]);
                    //printf("1: %d
    ",*(v.begin()+now-1));
                    v.erase(v.begin()+now-1);//删除已经被flag[]标记的
                    now = (now==tot?1:now);
                }
                else
                {
                    now%=tot;
                    now = (now==0?tot:now);
                    flag[v[now-1]]=1;
                    //printf(">>%d<<
    ",v[now-1]);
                    //printf("2: %d
    ",*(v.begin()+now-1));
                    v.erase(v.begin()+now-1);//删除已经被flag[]标记的
                    now = (now==tot?1:now);
                }
                tot--;//总数减一
            }
            for(i = 1; i <= 2*n; i++)
            {
                if(flag[i])
                    printf("B");
                else
                    printf("G");
                if(i%50==0) 
                    printf("
    ");
            }
            if((2*n)%50!=0) 
                printf("
    ");
            printf("
    ");
        }
        return 0;
    }



  • 相关阅读:
    java基本类型和引用做形参传递
    新阶段新开始
    给网页中的button加动画效果
    数组对象常用的几个函数总结
    微信小程序使用页面栈改变上一页面的数据
    pc端和移动端的“窗口”(viewport)故事(part1)
    js的for循环中出现异步函数,回调引用的循环值总是最后一步的值?
    macOS Sierra 如何安装任何来源的软件
    Box-shadow制作漂亮的外阴影输入框
    button标签与input type=button标签使用的差异
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/6911737.html
Copyright © 2011-2022 走看看