您的位置:首页 > 产品设计 > UI/UE

UVa 133 - The Dole Queue

2015-05-06 23:49 405 查看
题目:给你一串数字按照环形排列,每次向后数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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: