uva133 The Dole Queue 循环队列模拟

May 14, 2013
Uva

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=69 题目大意:   1到n按照逆时针的顺序围成一个环,一个人逆时针从1开始数k个数字,另一个人顺时针从n开始数m个数字,每次两个人最终数到的数字输出,并且把他们从原来的环里面删除,如果两个人数到的数字不同,输出一对,如果相同,输出这个数字。不管重复上面的做法,直到n个数字全部被删除。 题目思路:   模拟题,代码弱,写了很久,写不出来,就是感觉比较麻烦,看了人家的代码,做法很好!http://blog.csdn.net/actoy/article/details/8747826


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
typedef long long int LL;
const int MAXN =  0x7fffffff;
const int  MINN =  -0x7fffffff;
const double eps = 1e-9;
const int dir[8][2] = {{0,1},{1,0},{0,-1},{-1,0},{-1,1},
  {1,1},{1,-1},{-1,-1}};

int main(void){
#ifndef ONLINE_JUDGE
  freopen("uva133.in", "r", stdin);
#endif
  int n, k, m, i, j, pos, pos1, cnt, f[30], tmp;
  map<int, bool> mymap;
  while (~scanf("%d%d%d", &n, &k, &m)) {
    if (n+k+m==0) break;
    memset(f, 0, sizeof(f));
    for (i = 1; i <= n; ++i) f[i] = i;
    pos = 0; pos1 = n + 1; cnt = 0;
    while (cnt < n) {
      tmp = 0;
      while (1) {
        if (f[pos] != 0) tmp++;
        if (pos > n) pos = 0;
        if (tmp == k) {
          printf("%3d", f[pos]);
          cnt++; break;
        }
        pos++;
      }
      tmp = 0;
      while (1) {
        if (f[pos1] != 0) tmp++;
        if (pos1 <= 0) pos1 = n + 1;
        if (tmp == m) {
          if (f[pos] != f[pos1]) {
            printf("%3d", f[pos1]);
            cnt++;
          }
          break;
        }
        pos1--;
      }
      f[pos] = f[pos1] = 0;
      if (cnt < n) printf(",");
    }
    printf("\n");
  }

  return 0;
}

这种题目,就是考的代码和问题实现的方式,方法不对可能实现起来超级麻烦……   《夜莺》貌似第一次听这首曲子是4年前……

很好听~

comments powered by Disqus