码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 133 - The Dole Queue

时间:2015-05-07 00:57:53      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

题目:给你一串数字按照环形排列,每次向后数k个向前数m个的删掉,如果相同只删掉一个,输出删数字的顺序。

分析:约瑟夫环变形,模拟。直接模拟过程输出即可。

说明:注意%3d输出即可。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

int people[21];

int main()
{
	int n,k,m,left,right,last,count;
	while (cin >> n >> k >> m && n) {
		left = 1;right = 0;last = n;
		memset(people, 0, sizeof(people));
		while (last) {
			count = 0;
			while (count < m) {
				-- left;
				if (left < 1) 
					left = n;
				if (!people[left])
					++ count;
			}
			count = 0;
			while (count < k) {
				++ right;
				if (right > n) 
					right = 1;
				if (!people[right])
					++ count;
			}
			people[left] = people[right] = 1;
			if (left != right)
				printf("%3d%3d",right,left);
			else printf("%3d",left);
			last -= 1+(right!=left);
			if (last) printf(",");
		}printf("\n");
	}
    return 0;
}


UVa 133 - The Dole Queue

标签:

原文地址:http://blog.csdn.net/mobius_strip/article/details/45544207

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!