UVA 133 The Dole Queue

UVA 133 The Dole Queue

题意 输入N,k,m,有N个人站成一圈, k表示一个官员每次按k个位置顺时针找人,m表示一个官员按m个位置逆时针找人
每次找到的人就去除掉,并输出,直到没人。

解法 用数组模拟,每次寻找进行一次数组的变换,直到人数为0

代码
#include <iostream>
using namespace std;
#include <stdio.h>
#include <string.h>
#include <algorithm>

int vis[25];
int a[25];
int n, k, m;
int shun, ni;
void del(int x)
{
    for (int i = x; i < n ; i ++)
	a[i] = a[i + 1];
    n --;
}
int find(int x)
{
    for (int i = 1; i <= n ; i ++)
    {
	if (x == a[i])
	    return i;
    }
}
void tra()
{
    shun += k;
    while (shun > n)
    {	
	shun -= n;
    }
    ni -= m;
    while (ni < 1)
    {
	ni += n;
    }
    if (a[shun] == a[ni])
    {
	vis[a[shun]] = 1;
	int aa = a[shun];
	printf("%3d",  a[shun]);
	shun --;
	while (shun < 1)
	{
	    shun += n;
	}
	while (vis[a[shun]])
	{
	    shun --;
	}
	int aaa = a[shun];
	ni ++;
	while (ni > n)
	{
	    ni -= n;
	}
	while (vis[a[ni]])
	{
	    ni ++;
	}
	int bbb = a[ni];
	del(find(aa));
	shun = find(aaa);
	ni = find(bbb);
	if (n > 0)
	    cout << ",";
    }
    else if (a[shun] != a[ni])
    {
	vis[a[shun]] = 1;
	vis[a[ni]] = 1;
	int aa = a[shun];
	int bb = a[ni];
	printf("%3d%3d", a[shun], a[ni]);
	shun --;
	while (shun < 1)
	{
	    shun += n;
	}
	while (vis[a[shun]])
	{
	    shun --;
	}
	ni ++;
	while (ni > n)
	{
	    ni -= n;
	}
	while(vis[a[ni]])
	{
	    ni ++;
	}
	int aaa = a[shun];
	int bbb = a[ni];
	del(find(aa));
	del(find(bb));	
	shun = find(aaa);
	ni = find(bbb);
	if (n > 0)
	    cout << ",";
    }
}
int main()
{
    while (scanf("%d%d%d", &n, &k, &m) != EOF && n && k && m)
    {
	shun = n; ni = 1;
	memset(vis, 0, sizeof(vis));
	memset(a, 0, sizeof(a));
	for (int i = 1; i <= n; i ++)
	    a[i] = i;
	while (n > 0)
	{
	    tra();
	}	   
       cout << endl;	
    }
    return 0;
}


你可能感兴趣的:(ACM,uva)