uva - 133 The Dole Queue(成环状态下的循环走步方法)

类型:循环走步

 1 #include <iostream>
 2 #include <sstream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <cctype>
10 #include <algorithm>
11 #include <cmath>
12 #include <deque>
13 #include <queue>
14 #include <map>
15 #include <stack>
16 #include <list>
17 #include <iomanip>
18
19 using namespace std;
20
21 #define INF 0x7fffffff
22 #define maxn 1010
23 typedef unsigned long long ull;
24
25
26 int arr[maxn];
27 int N, k, m;
28
29 //d也能表示顺时针还是逆时针,顺时针-1,逆时针1
30 //注意此处的算法
31 int go(int t, int p, int d)
32 {
33     while(t--)
34     {
35         do{
36             ///
37             p = (p+d+N-1)%N+1;
38         }
39         while(!arr[p]);
40     }
41     return p;
42 }
43
44 int main()
45 {
46     while(scanf("%d%d%d", &N, &k, &m) && (N+k+m))
47     {
48         memset(arr, 0, sizeof(arr));
49         for(int i = 1; i <= N; i++)
50             arr[i] = i;
51
52         int left = N;//剩下的人数
53         int p1 = 0, p2 = N+1;//位置标记
54
55         while(left)
56         {
57             p1 = go(k, p1, 1);
58             p2 = go(m, p2, -1);
59             arr[p1] = arr[p2] = 0;
60             //注意此处的输出优化
61             printf("%3d", p1);  left--;
62             if(p1 != p2)
63             {
64                 printf("%3d", p2);  left--;
65             }
66             if(left)    printf(",");
67                 else printf("\n");
68         }
69     }
70     return 0;
71 }
72             
时间: 2024-11-07 04:51:11

uva - 133 The Dole Queue(成环状态下的循环走步方法)的相关文章

UVa 133 The Dole Queue

 The Dole Queue  In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone i

UVA - 133 The Dole Queue(模拟链表)

点击打开链接 n的人围成一个环,然后按逆时针编号1-n,一个人从1开始逆时针数k个数,另一个人从N开始顺时针数m个数,然后 数出来的两个人出列(两个人可能一样)出列,然后继续此过程,直到全部人都出列为止. 思路是用循环链表来模拟,注意 要分情况来讨论. #include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #inclu

133 - The Dole Queue

The Dole Queue In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone is

The Dole Queue UVA - 133

In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone is arbitrarily cho

uva The Dole Queue

题目如下: The Dole Queue  In a serious attempt to downsize (reduce) the dole queue, The NewNational Green Labour Rhinoceros Party has decided on the followingstrategy. Every day all dole applicants will be placed in a largecircle, facing inwards. Someone

UVA 133(循环链表)

C - The Dole Queue Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description  The Dole Queue  In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided

uva 11825 ,Hacker&#39;s Crackdown 状态压缩 dp

// uva 11825 Hacker's Crackdown // // 题目意思看了很久才看懂,有n台计算机,有n种服务,每台计算机上运行所有 // 的服务,并且其中有的计算机与某些计算机相互邻接,对于每台计算机, // 你可以选择一项服务,停止这项服务,则与它邻接的计算机的该服务也停止了 // 你的目的是让经量多的服务完全瘫痪 // // 换而言之,这个问题就是在n个集合中(p[1]....p[n])分成尽量多的组数,使得每组 // 的并集等于全集(即所有的n台电脑都停止)... // /

C++学习:任意合法状态下汉诺塔的移动(原创)

汉诺塔问题: 问题是源于印度一个古老传说的益智玩具.大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘.大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上.并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘. 现在将问题变形为:初识时,n个金盘分散摆放在三根柱子上,并且所有金盘都处于合法状态,将这些分散的金盘全部移动到第三根柱子上,并打印每一次的移动步骤以及移动后三个柱子上金盘的状态. C++实现代码如下: 1 #ifnde

The Dole Queue

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit cid=1036#status//A/0" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="font-family:Verdana,Arial,sans-serif; font-si