Uva133 - The Dole Queue

题意

n个人围城一个环,逆时针编号1~n,A从1开始逆时针数K个,B从n顺时针数M个,被选中的1或2个人一次领救济金,输出顺序

思路

双向约瑟夫环,公式没推出来,用的模拟

总结

开始用的数组模拟链表写的,WA了,打算再改改。不知道为什么这道题卡了很久总不对。

模拟:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn = 25;
 7 int n, k, m;
 8 int a[maxn];
 9 int main()
10 {
11     //freopen("in.txt", "r", stdin);
12
13     while(cin >> n >> k >> m && n){
14         for(int i = 1; i <= n; i++)
15             a[i] = i;
16         int left = n;
17         int p1 = 1, p2 = n;
18         while(left){
19             int cnt = 0;
20             for(; cnt != k;p1++){
21                 if(p1 == n+1) p1 = 1;
22                 if(a[p1]) cnt++;
23
24             }
25             p1--;
26             for(cnt = 0; cnt != m; p2--){
27                 if(p2 == 0) p2 = n;
28                 if(a[p2]) cnt++;
29
30             }
31             p2++;
32             printf("%3d", p1);
33             left--;
34             if(p2 != p1){
35                 printf("%3d", p2);
36                 left--;
37             }
38             a[p1] = a[p2] = 0;
39             if(left) printf(",");
40         }
41         printf("\n");
42     }
43     return 0;
44 }
时间: 2024-08-05 23:11:32

Uva133 - The Dole Queue的相关文章

uva133 The Dole Queue ( 约瑟夫环的模拟)

题目链接: 啊哈哈,选我选我 思路是: 相当于模拟约瑟夫环,仅仅只是是从顺逆时针同一时候进行的,然后就是顺逆时针走能够编写一个函数,仅仅只是是走的方向的标志变量相反..还有就是为了(pos+flag+n-1)%n+1的妙用... 题目:  The Dole Queue  In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decid

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

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

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

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

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

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

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 &

算法练习(一)

昨天是个值得纪念的日子,我数学建模拿了推荐国家一等奖的名额,希望最后能顺利拿到国一吧.现在大三已经开学一个月了.这一个月因为社会实践评优的事情真的很忙,还好最后拿到了可能拿到的所有的奖项.结果自己把科研助手这件事给耽误了,今天去找马老师,结果马老师的实验室人已经满了.所以没办法,我可能又要去找其他老师了. 今年国家奖学金的名额里面没有我,没有就算了吧.卧薪尝胆,好好学习,这一学期至关重要.所以自己现在就要开始准备保研的机试,现在的训练非常重要,无论如何,这是自己未来要走的一步路.我现在最重要的就