首页 > 其他 > 详细

HDU-4841 圆桌问题 STL模拟约瑟夫问题

时间:2014-09-03 22:43:57      阅读:384      评论:0      收藏:0      [点我收藏+]

中文题,题意一看就是卧槽,这不约瑟夫么,然后脑子一抽就用了链表写,然后果然T了,最后用Vector模拟的约瑟夫问题。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <vector>
#define LL __int64
using namespace std;
int n,m;
vector<int>p;
int flag[55000];
int main()
{
	int cnt;
	int s;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		s=2*n;
		p.clear();
		for(int i=1;i<=s;i++)
		{
			p.push_back(i);
			flag[i]=0;
		}
		cnt=1;
		while(s>n)
		{
			cnt+=m-1;
			if(cnt<=s)
			{
				flag[p[cnt-1]]=1;
				p.erase(p.begin()+cnt-1);
				if(cnt==s)
				{
					cnt=1;
				}
			}
			else
			{
				cnt%=s;
				if(cnt==0)
				{
					cnt=s;
				}
				flag[p[cnt-1]]=1;
				p.erase(p.begin()+cnt-1);
				if(cnt==s)
				{
					cnt=1;
				}
			}
			s--;
		}
		for(int i=1;i<=2*n;i++)
		{
			if(flag[i])
			{
				cout<<"B";
			}
			else
			{
				cout<<"G";
			}
			if(i%50==0)
			{
				cout<<endl;
			}
		}
		cout<<endl;
		cout<<endl;
	}
	return 0;
}


HDU-4841 圆桌问题 STL模拟约瑟夫问题

原文:http://blog.csdn.net/q295657451/article/details/39034759

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!