首页 > 代码库 > uva - 133 The Dole Queue(成环状态下的循环走步方法)

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 0x7fffffff22 #define maxn 101023 typedef unsigned long long ull;24 25 26 int arr[maxn];27 int N, k, m;28 29 //d也能表示顺时针还是逆时针,顺时针-1,逆时针130 //注意此处的算法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             

 

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